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

Message ListMessage List     Post MessagePost Message

  XY chart axis number format. Help!
Posted by Tony on Oct-27-2021 22:46
Hello,

I'm sure I can do this, but I cannot figure out from the help system or mfcdemo how to do it.

What I'd like to do is format the numbers on the Y axis of a graph to reflect the size of the numbers being plotted. So if there are six zeros, I'd like to see "1 M" instead of "1000000".

I'm certain I can use setMultiFormat, setMultiFormat2 and setFormatCondition to do this. I just cannot figure out what to do based on the documentation.

Thanks in advance,

Tony.

  Re: XY chart axis number format. Help!
Posted by Tony on Oct-27-2021 23:11
Ignore me again, I've had a trial and error session and managed to figure things out.

If anyone else needs it, the code below works well.

Tony.

c->yAxis()->setFormatCondition("<", 1000);
c->yAxis()->setLabelFormat("{value}");
c->yAxis()->setFormatCondition("<", 1000000);
c->yAxis()->setLabelFormat("{={value}/1000} K");
c->yAxis()->setFormatCondition("<", 1000000000);
c->yAxis()->setLabelFormat("{={value}/100000} M");
c->yAxis()->setFormatCondition("<", 1000000000000);
c->yAxis()->setLabelFormat("{={value}/1000000000} B");
c->yAxis()->setFormatCondition("else");
c->yAxis()->setLabelFormat("{={value}/1000000000000} T");

  Re: XY chart axis number format. Help!
Posted by Peter Kwan on Oct-27-2021 23:30
Hi Tony,

If your code specifies the y-axis scale (eg. with Axis.setLinearScale or Axis.setLinearScale2), you can always specify the labels you want to use. So for your current enquiry, I assume you are using ChartDirector auto-scaling.

For your case, there are two methods:

(1) You can use Axis.setFormatCondition to set the conditions for a particular format. It is like a "If" statement. The format is applied only if the condition is true. You can have a list of conditions and formats. For example:

// If all labels are multiples of 500000
c->yAxis()->setFormatCondition("align", 500000);
c->yAxis()->setLabelFormat("{={value}/1000000} M");

// If all labels are multiples of 500
c->yAxis()->setFormatCondition("align", 500);
c->yAxis()->setLabelFormat("{={value}/1000} K");

// else
c->yAxis()->setFormatCondition("else");
c->yAxis()->setLabelFormat("{value}");

See:

https://www.advsofteng.com/doc/cdcpp.htm#Axis.setFormatCondition.htm


(2) Your code can ask ChartDirector what are the label positions using Axis.getTicks, and then tell ChartDirector what labels to use for those positions (using Axis.addLabel). This allows you can use your own code to determine which format to use. It is like:

.... create chart as usual ....

//After entering all the data and configuration, auto-scale the axes
c->layoutAxes();

//Get the ticks
DoubleArray ticks = c->yAxis()->getTicks();

//Format the labels, assuming  std::string myFormatFunction(double t) is the
//custom formatting function
for (int i = 0; i < ticks.len; ++i) {
    std::string label = myFormatFunction(ticks[i]);
    c->yAxis()->addLabel(ticks[i], label.c_str());
}


Regards
Peter Kwan