|
Gridline problem (finance chart) |
Posted by Brian Hui on Apr-29-2023 11:36 |
|
Hi Peter,
I am developing two finance charts, side-by-side. The left one is 5min and the right one is 30min intervals. However, the vertical gridline showing is different. I think the vertical grid line distance is depending on the x-axis value. For better looking, can change to a fixed grid line distance instead? Or simply remove the vertical gridline. Thanks a lot!
Regards,
Brian
|
Re: Gridline problem (finance chart) |
Posted by Peter Kwan on Apr-29-2023 17:13 |
|
Hi Brian,
Yes, you can have complete control of the labelling and the grid lines.
By default, ChartDirector will try to put reasonable number of labels on the x-axis and align them with some common time units, such as one label per hour, or one label per month, etc.. It depends on the number of pixels available on the chart, and the time range of the chart. For example, if the time range is one year, it may use monthly labels. If the time range is one day, it may use hourly labels or bi-hourly labels.
If you want the grid lines to always at the same position regardless of the time range, you can set the labels with your own code. The x-axis labels come from the first chart added to the FinanceChart, which in your case in the main price chart. You can use Axis.Labels of the main price chart to set the x-axis labels. The exact code depends on your programming language. For example, in C#, it is like:
... create FInanecChart and add data as usual ...
// Add the main price chart
XYChart p = c.addMainChart(240);
// Create labels array you want to use. The array size should be the same as the
// timeStamps array. If there is no label at some positions, simply put an empty string
// there.
String[] labels = new string[timeStamps.Length];
.... fill the labels array ....
// Use the label array for the x-axis.
p.xAxis().setLabels(labels);
Every labelled position will have a grid line. If you want a grid line, but does not want the label, you can use space character " " as the label.
Best Regards
Peter Kwan |
Re: Gridline problem (finance chart) |
Posted by Brian Hui on Apr-29-2023 23:48 |
|
Hi Peter,
Thanks for your great help! After I added the setLabels into the code (see below), the vertical grid lines were removed successfully. However, the time showing in the legend becomes incorrect (showing 0001-01-01 00:00, before should be yyyy-mm-dd hh:mm). Thanks!
Below are the added code
===============
string[] labels = new string[vpTimeStamps.Length];
for (int ll = 0; ll < vpTimeStamps.Length; ll=ll+20)
{
labels[ll] = "";
}
mainChart.xAxis().setLabels(labels);
===============
Regards,
Brian
|
Re: Gridline problem (finance chart) |
Posted by Peter Kwan on Apr-30-2023 02:25 |
|
Hi Brian,
I overlooked that your chart is using track cursors. In this sample code, the date/time in the track cursor is obtained from the x-axis using code like:
c.xAxis().getFormattedLabel(xValue, "mmm dd, yyyy")
So modifying the xAxis labels may affect the track cursor date/time label as well.
If your objective is to hide all the labels and grid lines, there is a simple method. You can put the following line *before* FinanceChart.setData.
c.setDateLabelFormat("~", "~", "~", "~", "~", "~", "~");
The will set all the possible formats to invisible without affecting the x-axis, so it would not affect the track cursor.
If you want to have detail control of the labels (eg. make some labels visible, and some invisible), it is necessary to modify the x-axis labels. We would then need to find another way for the track cursor to obtain the date/time. I think of two alternatives:
(a) Obtain the date/time directly from the timeStamps array, like:
c.formatValue(timeStamps[xValue - extraPoints], "mmm dd, yyyy")
(b) We can put the timestamps in another invisible axis, and use that axis for the track cursor.
// p = main price chart
p.xAxis2().setLabels(timeStamps); // the second x-axis
p.xAxis2().setLabelFormat("~"); // all labels of xAxis2 are invisible
Then in track cursor code uses the xAxis2 instead of xAxis:
c.xAxis2().getFormattedLabel(xValue, "mmm dd, yyyy")
Best Regards
Peter Kwan |
Re: Gridline problem (finance chart) |
Posted by Brian Hui on Apr-30-2023 12:46 |
|
It works by using the axis2 method! Many thanks! =) |
|