ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  How to show minimum and maximum lines on a line chart?
Posted by Electron Shepherd on Jul-21-2016 06:54
Attachments:
I took the sample code for the Simple Line Chart, changed some values to be 100 and some to be 0, and added a call to setLinearScale to for the y axis to use a 0 - 100 range. Full code is below:

    // The data for the line chart
    double data[] = {30, 28, 40, 55, 75, 100, 100 ,100, 100, 100 ,100, 65, 75, 0, 0, 0, 0, 0, 0, 0,
        56, 48, 52, 65, 62};

    // The labels for the line chart
    const char *labels[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12",
        "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"};

    // Create a XYChart object of size 250 x 250 pixels
    XYChart *c = new XYChart(250, 250);

    // Set the plotarea at (30, 20) and of size 200 x 200 pixels
    c->setPlotArea(30, 20, 200, 200);

    // Add a line chart layer using the given data
    c->addLineLayer(DoubleArray(data, (int)(sizeof(data) / sizeof(data[0]))));

    // Set the labels on the x axis.
    c->xAxis()->setLabels(StringArray(labels, (int)(sizeof(labels) / sizeof(labels[0]))));

    // Display 1 out of 3 labels on the x-axis.
    c->xAxis()->setLabelStep(3);

c->yAxis()->setLinearScale(0, 100);

    // Output the chart
    c->makeChart("c:simpleline.png");

    //free up resources
    delete c;

When I run this, I get the attached chart. Notice that the line at 100 and the line at 0 are "invisible".

How can I get the chart to display the whole line, including the line for the points at 0 and 100? I'm aware of the ability to auto-scale the axis, but in my case, the graph I'm actually generating has a percentage value on the Y axis, and showing an axis value of  more than 100% would not be acceptable. In any case, the line at 0 is still not displayed.

How can I have a line chart that goes from 0 - 100 on the Y axis, and has data values from 0 - 100, and have the whole line displayed?
simpleline.png

  Re: How to show minimum and maximum lines on a line chart?
Posted by Peter Kwan on Jul-21-2016 12:47
Hi Electron Shepherd,

The easiest method is to add a 1-pixel margin between the end point of the axis and the plot area border. It is like:

c->yAxis()->setMargin(1, 1);

Another method is to set the axis and the plot area border to be transparent. It is like:

c->setPlotArea(30, 20, 200, 200, -1, -1, Chart::Transparent);
c->xAxis()->setColors(Chart::Transparent, 0x000000, 0x000000, 0xcccccc);
c->yAxis()->setColors(Chart::Transparent, 0x000000, 0x000000, 0xcccccc);

Hope this can help.

Regards
Peter Kwan

  Re: How to show minimum and maximum lines on a line chart?
Posted by Electron Shepherd on Jul-21-2016 17:00
Peter,

Unfortunately, neither of those options will work for us. The line width is user-selectable up to 5 pixels, so we would need a large margin either side to be sure of being able to see the whole line, which will make the graph appearance unacceptable when thinner, 1 pixel lines are used.

Also, since we use setClipping(0), even if we make the axis transparent (which is not really acceptable visually either), we still won't see all of the thicker line.

Is there some way that I can determine the pixel location at which a data point will be plotted?

I'm thinking that if I can do that, I can then modify my data values before I add them to the dataset, reducing the 100 slightly, and increasing the 0 slightly, so that when ChartDirector plots them, both actually appear on the chart.

I have looked at DataSet.getPosition, but from the documentation, it seems that this function will simply return the data value, not the plotted location.

  Re: How to show minimum and maximum lines on a line chart?
Posted by Peter Kwan on Jul-21-2016 23:35
Hi Electron,

If the data value is 0, the y-position of the data point is at y = 0 (the bottom of the axis), which is equal to the plot area top + the plot area height (both supplied by your code).

If you want to adjust the data point, adjusting by 3 pixels is equivalent to adjust the data value by 3.0 x plot_area_height / 100 (as your axis is always from 0 to 100). So basically, you just make sure all data points are at least 3.0 x plot_area_height / 100 in value.

I think there are draw backs to adjusting the data points as well. It makes small data values indistinguishable. For example, 1% and 0% may become the same minimal value after adjustment. So a non-horizontal line joining y = 1% with y = 0% will be the indistinguishable from a horizontal line joining 0% to 0%, and also indistinguishable from a horizontal line joining 1% to 1%.

If you think accuracy within 1% is important, personally, I think it is better to plot 0% at the 0% position without adjustment. That means half of the line width must go under y = 0.

May be you can try the following method:

- Use setMargin to set top and bottom margins to be 1 + half the line width. This will allow the line to be plotted at the accurate position

- If you do not want the border of the plot area to be under y = 0, you can set the border and the axis to be transparent as mentioned in my previous message. Note that even if the axis is transparent, there would still be a y = 0 grey grid line at the axis position, which looks like a grey axis at y = 0.  The axis labels in this case will still be at the plot area border position. If you want to move the axis labels to the y = 0 position (which is now inside the plot area), you can use:

c->setAxisAtOrigin(Chart::XAxisAtOrigin);

- If you want to use other color for the axis, you can always use Axis::addMark to add a mark line at y = 0. The mark line can be configured to stay behind the plotted line (using Mark::setMarkOnTop(false)).

- If you want to have some boundary (for the plot area border) for the chart, you can always add lines by using BaseChart::addLine and use Line::setZOrder to configure it to be under the plotted line but above the grid lines.


Regards
Peter Kwan

  Re: How to show minimum and maximum lines on a line chart?
Posted by Electron Shepherd on Jul-22-2016 21:17
Peter

Many thanks for the detailed help. We set the axes to transparent, and then add them "manually" with four calls to addLine() and change the Z-Order so that the chart lines plot over the top. This gives us exactly the result we want.