ChartDirector Support Forum Home   Search
Line color based on +/- zero?
Posted by Steve on Sep-10-2021 02:38
I have a line chart that plots positive and negative values over time. Is there some way to color the line based on whether values are positive or negative?

I saw the example for a multi-color line chart https://www.advsofteng.com/doc/cdperl.htm#multicolorline.htm But that seems to assign colors to line segments.  That's not exactly what I want because a given segment might cross from + to - or vice versa.

Re: Line color based on +/- zero?
Posted by Peter Kwan on Sep-11-2021 03:03
Hi Steve,

You can use a y-zone color as the line color:

\$myLineColor = \$c->yZoneColor(0, 0xff0000, 0x00cc00);

See:

Regards
Peter Kwan

Re: Line color based on +/- zero?
Posted by Steve on Jan-10-2022 03:24
So how do I do this for a secondary axis?  I've tried the below, but it doesn't work:

\$c = new XYChart(800, 480);
\$c->setPlotArea(50, 20, 700, 350);
\$c->addTitle("New Cases & New COVID-19 Beds 7-Day Avg", "Arial Bold", 10);
\$tb = \$c->xAxis()->setLabels(\$labels);
\$tb->setFontAngle(90);
\$tb->setFontSize(8);
\$c->yAxis()->setTitle("New Cases");
\$c->yAxis()->setColors(\$black, \$black, \$black);
\$c->yAxis2()->setTitle("New Beds");
my \$a2 = \$c->yAxis2()->setColors(\$black, \$black, \$black);
my \$myLineColor = \$a2->yZoneColor(0, \$green, \$red);
\$c->makeChart("casevbeds.png");

Re: Line color based on +/- zero?
Posted by Peter Kwan on Jan-10-2022 12:35
Hi Steve,

The issue is in the line:

my \$a2 = \$c->yAxis2()->setColors(\$black, \$black, \$black);

The Axis object is the returned value of yAxis2, not setColors. You can modify it to:

my \$a2 = \$c->yAxis2();
\$a2->setColors(\$black, \$black, \$black);

Regards
Peter Kwan

Re: Line color based on +/- zero?
Posted by Steve on Jan-10-2022 23:13
When I change code to

my \$a2 = \$c->yAxis2();
\$a2->setColors(0x000000, 0x000000, 0x000000);
my \$myLineColor = \$a2->yZoneColor(0, \$green, \$red);

I get an error on the last line:

unknown method Axis.yZoneColor

Re: Line color based on +/- zero?
Posted by Peter Kwan on Jan-11-2022 00:59
Hi Steve,

Sorry, the yZoneColor line should be:

my \$myLineColor = \$c->yZoneColor(0, \$green, \$red, \$a2);

Regards
Peter Kwan