Working with Steema TeeChart graphs. (In particular objects: TChart, TChartSeries, TBarSeries, TLineSeries)
My task is to dynamically combine graphs when user selects some of the them, their values are added or subtracted based on XLabel value.
So this is what I get after selecting a couple of graphs and then deselecting them (which does the following:
Sets Series with DefaultBar/LineSeries
Adds values of selected graphs to the DefaultBar/LineSeries
Subtracts values of deselected graphs from the DefaultBar/LineSeries)
Result (logically) has to be 0 for all XLabels
But here is what I get:
Same graph without marks
Here are some of the values:
Can I somehow keep the actual values instead of rounded values (the ones represented on the graph), so that I will not have any rest after subtraction procedure?
I could do something like this to get rid of the rest (I know that the values will be big):
//bad solution
for Serie := 0 to Chart.SeriesCount - 1 do
for YValue := 0 to Chart.Series[Serie].YValues.Count - 1 do
if StrToFloat(FormatFloat('0.00', Chart.Series[Serie].YValue[YValue])) = 0 then
Chart.Series[Serie].YValue[YValue] := 0;
But this is not a very great solution - not scalable.
Probably related:
When I select only 1 graph and deselect it, then all the values are 0, the problem occurs when multiple graphs are selected (addition occurs)
Related
Currently I am working on already established application which uses Core plot and in specific its using ScatterPlot to draw a graph. This graph is drawn with values obtained from connected bluetooth device so the graph is dynamically drawn.
XY graph with X axis having time value and Y axis having value from BLE device.
Now the requirement is to add an out of range value with a label "OL" in the graph and its not a fixed constant value for the bluetooth device(Its more of a state set in model object that is constructed in BLE layer with junk value obtained for that state and on which we can figure out its a out of range value).
So for e.g., if I get 'OL' value initially then I need to add it on graph above 0 with label "OL". '-OL' below 0 with label "-OL". The values obtained from BLE is continuously added to array and the graph is redrawn using this array.
So behaviour is expected as below:
If I get 200 as value in the array and if the array has OL value then OL value has to be plotted above 200 and if the next value from BLE is 300 which will be added to array and since OL is above 200, now OL has to be plotted above 300.
I tried 2-3 different approaches to figure out a solution for this but with no success.
Image of the expected requirement:
Require someone to help out if its possible to implement this using Core plot.
You'll need a second scatter plot for the red lines. In the main plot, return nil from the datasource for the values that are outside the valid range. This will leave gaps in the line, to be filled in with the other plot.
For the second plot, return nil from every data index, except ones that are out of the valid range. Return the range limit value for those points. You'll also need to return the correct in-range value for the two points on either side of the out-of-range values to connect the line to the main plot line.
I’ve got my TeeChart looking pretty good, the grid lines / ticks appear where I wish and so forth (the X Axis LabelStyle is set to talAuto). Now I need to format the X Axis labels so they display a time offset rather than the data point number.
I’ve changed the LabelStyle to talMark and am handling the chart’s OnGetAxisLabel. The problem is that my event handler is called for every X Axis point rather than just the points which displayed labels when I had LabelStyle set to talAuto.
How can I set things up so my event handler is only called for the labels which were drawn when LabelStyle was talAuto?
I’m using C++ Builder XE3 and the version of TeeChart included with IDE.
UPDATE inspired by Yeray's comment:
With LabelStyle set to talAuto I always get ValueIndex of -1 passed to my event handler. In looking at the LabelText, I see values that I would have expected in ValueIndex, but I also get an equal number of incorrect values (ignoring non-X Axis values). For example...
My chart displays 5 X-Axis values: 200, 400, 600, 800, 1000. My event handler is called 10 times, in this order:
ValueIndex LabelText
-1 937.5
-1 0
-1 240,000
-1 200
-1 239,800
-1 1,000
-1 800
-1 600
-1 400
-1 200
I have no idea where the first 5 values came from or why my event handler was called.
NOTE: I removed the C++ Builder tag because this question might be answered by anyone using TeeChart regardless of their language; the fact that I'm using BCB is not important to the question.
I’ve changed the LabelStyle to talMark and am handling the chart’s
OnGetAxisLabel. The problem is that my event handler is called for
every X Axis point rather than just the points which displayed labels
when I had LabelStyle set to talAuto.
Right, using the talMark LabelStyle, the axis will draw as many labels as points in the series.
You can use the talAuto LabelStyle to get the number of labels you wish and you can still format the labels in the OnGetAxisLabel event.
UPDATE:
When I use talAuto, ValueIndex is always -1 so I have no idea which
data point is associated with the event.
When talAuto is set and the series has labels, it behaves like talText and talMark: these strings are used in the bottom axis and the ValueIndex in OnGetAxisLabel event can be used.
When talAuto is set but the series has no label, it behaves like talValue: the bottom axis calculates the labels to show in function of the Minimum, Maximum and Increment. The ValueIndex in OnGetAxisLabel event can't be used because the axis labels don't correspond to a series point.
I guess you don't have labels in your series. Otherwise, changing from talAuto to talMark shouldn't change anything.
So you have to decide between one or the other. Maybe you can use talValue (or talAuto without labels) to get the number of labels you wish, and extract the info you need from the string that is going to be drawn.
Alternatively, it could be easier to use CustomLabels. They will allow you to control both the positions and the text of the axis labels without needing any event. In example:
procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin
Chart1.View3D:=false;
Chart1.AddSeries(TBarSeries).FillSampleValues;
Chart1.Axes.Bottom.Items.Clear;
for i:=0 to Chart1[0].Count-1 do
Chart1.Axes.Bottom.Items.Add(Chart1[0].XValue[i], 'label ' + IntToStr(i));
end;
This question relates to another ongoing question here: Format Stones and Pounds correctly?
I figured it would be more suitable to ask here as a separate question, before deciding what to do.
As described from the link above, I am representing weight in a TChart as stones and pounds.
Before I decide on whether to change the data type I am currently using (Double) as the comments from above suggest, I want to see how I can display my chart in Stones and Pounds as I originally had in mind.
See this sample image I manipulated for demo purposes:
The problem is the left Axis of the chart, as the notes in the image state - the chart increments up to .99 (Pounds) before reaching the next whole number (Stones in this case). For example:
03.00
02.99 < .99 = max number before reaching 3.00
02.98
02.97
I need the Left axis to be setup like so:
03.00
02.13 < .13 = max number before reaching 3.00
02.12
02.11
02.10
And also display all the labels for the pounds part.
Is there a setting somewhere that I can use to set the max value for the .00 (lbs) before reaching the next whole number (stones)?
At the moment nothing seems to be making a lot of sense, From what I understand from the other question is I should be using Integers and representing as Pounds only.
I want to see how it could look before making any changes though, ideally I want the chart to stay near the same as the image if possible.
Hope this makes sense.
Thanks.
UPDATE
Just thought I would update the changes I eventually got working.
All credit has to go to David Heffernan for his persistence in explaining where I was going wrong, and writing detailed information and advice.
There is still some work I need to do, but for now here is the updated chart I now have:
I have not done the values in labels, but from the sample image above this is the values I used:
15.2
15.3
15.11
15.13
16.0
16.4
You can't use your current representation to plot a reasonable chart. Suppose you have a bar chart with the following values:
10st 12lb
10st 13lb
11st 0lb
11st 1lb
The chart will look like this:
Forget about the axis labelling for a moment. Look at the difference between the bars. The difference between each adjacent value is 1lb. But the graph tells a completely different story. I've used a bar chart here, but the principle applies for all chart types.
The bottom line is that you need to represent your weight values with a true decimal representation of the weight. Your representation does not obey basic laws of arithmetic. So, half a stone must be 0.5. Trying to represent a half with the number 0.07 is simply asking for a world of pain.
Use this formula to convert from stones and pounds to a true floating point representation of the weight:
FloatingPointWeightValue := Stones + Pounds/14.0;
Or if you store only the pounds then it is simply
FloatingPointWeightValue := Pounds/14.0;
I would suggest that you make sure that the Increment of the axis is at least 1. If you need to go smaller than that then you'll have to get into custom axis label titles. And when you add your values, give them a label. This will allow the chart to use sensible labels to identify values. For example:
Series1.Add(10 + 12/14, '10st, 12lb');
Series1.Add(10 + 13/14, '10st, 13lb');
Series1.Add(11 + 00/14, '11st, 0lb');
Series1.Add(11 + 01/14, '11st, 1lb');
And the result looks like this:
You'd obviously write a bunch of helper functions to handle this. I'd suggest storing the raw data as pounds. So, 11st 1lb would be stored as 11*14 + 1 = 155. Then you'd have these helpers:
function StonesFromPounds(Pounds: Integer): Double;
begin
Result := Pounds / 14.0;
end;
function StonesPoundsLabel(Pounds: Integer): string;
begin
Result := Format('%dst %dlb', [Pounds div 14, Pounds mod 14]);
end;
And then the data population would look like this:
Pounds := 152;
Series1.Add(StonesFromPounds(Pounds), StonesPoundsLabel(Pounds));
You have complete freedom with how you create text labels for your weights. If you want something less verbose than 10st 13lb then you can adjust the helper functions to your needs.
Now, this has been quite a long answer, so I will summarise my advice:
When you acquire the data, from what ever source, convert from whichever format they arrive in to a well-defined storage format, the raw data format.
Store this raw data as integer values, in units of pounds.
When you plot the weights, convert to floating point values in units of stones by Stones := Pounds/14.0.
Associate text labels to each value that you plot so that you can present the weights in human readable form.
Be prepared to supply custom label axes if you need to label weights in between whole values of stones.
Introduction
I am using the TChart component and am finding the options rather confusing and hard to find what I need.
The Left Axis of the chart has a minimum of 0 and a maximum of 5000, the Bottom Axis has a minimum of 0 and a maximum of 52 (weeks in a year).
In my chart I have 2 series that are populated based on values from a record I have created which can be edited at runtime, everything is working good so far.
Problem
Take a look at the below image of part of the chart filled with some random values:
I am not sure if such an option exists or not, but notice from week 4 onwards where no values have been added there is a solid red line.
I don't want the chart to plot values that are less than 0, so in this example from week 4 onwards there should be no more lines (I can confirm that from week 4 onwards I have the values set at -1)
Week 1 to 4 does not show this bottom red line, obviously because the values are greater than 0 and are therefore plotted above the line, but this should make my problem a bit more clearer.
So, how do I prevent the chart from plotting / drawing values that are less than 0, importantly for the Bottom Axis of the chart?
I am sure there must be a simple option somewhere, I just cannot find it if there is one as I am not too familiar with the TChart Component.
You have the possibility when adding values to a series to add them as null.
This will prevent them from showing.
Ken is correct saying that a tricky situation is to handle a single value surrounded by null values.
Picking another series type than FastLine and show dots as well as lines might work better.
For a FastLine series to show gaps, set:
Series1.IgnoreNulls := False;
And to discriminate values below zero:
if (y < 0) then
Series1.AddNullXY(x, y, '')
else
Series1.AddXY(x, y, '');
if y < 0 then
LineSeries1.AddXY(x, y, '', clNone)
else
LineSeries1.AddXY(x, y, '', clRed);
I have a chart that I would like to display based on a date range from the user. This particular chart has a data point for every 15 minutes. So there can be a lot of data points for each series if a users selects a large date range. Here is a couple of examples:
623 data points in a series
1470 data points in a series
In the first example the chart does display. In the second example the chart does not display. There is a Highstock demo (52,000 points with data grouping) that works with a lot of data points. I have tried to change the above charts to a highstock chart and still have the same results.
What can I do to fix this?
This is due to the turbo threshold option:
"When a series contains a data array that is longer than this, only one
dimensional arrays of numbers, or two dimensional arrays with x and y
values are allowed. Also, only the first point is tested, and the rest
are assumed to be the same format. This saves expensive data checking
and indexing in long series."
It is set to 1000 points by default. Your chart is not rendering because each point in your series is an object and their number is greater than the threshold.
Here's a jfFiddle demonstrating your plot working with the threshold set to 2000.
Here's the modified section of code:
plotOptions: {
spline: {
turboThreshold: 2000,
...
Another solution would be to encode your series data in a 2-d array instead of having each point represented by and object with x-y properties.
a workaround for turboThreshhold is something like this if you generation your response with PHP:
if(count($responseObj) > 1000){
$modolo = round(count($responseObj) / 1000);
for($i = count($responseObj)-1; $i >= 0 ; $i--){
if(($i % $modolo) != 0){
unset ($responseObj[$i]);
}
}
$responseObj = array_merge($responseObj);
}