How to format DataLabels and AxisLabels differently for positive and negative values in ChartDirector for ASP?

60 Views Asked by At

I'm using ChartDirector for ASP creating a ChartXY with 2 y axes, one for each array of data, one represented by BarChart and the other by LineChart. All values that I use in these cited are numerical values and I need to format the DataLabels and Labels of the Axes differently for positive and negative values.

I've tried many ways, and the main reason is that the methods for setting the formatting of the labels only accept a string and not a function (as is possible in ChartC3 for example), as shown in the documentation: https://www. advsofteng.com/doc/cdcom.htm#paramsub.htm

Formatting example:

data: 88.9 label: 88.9
data: -88.9 label: (88.9)

my code:

Set cd = CreateObject("ChartDirector.API")

Set c = cd.XYChart(800, 440)
Call c.setPlotArea(30, 30, 740, 360)

Call c.xAxis().setLabels(labels)

Set barLayer = c.addBarLayer(data0, &H444466)
Call barLayer.setAggregateLabelStyle()
Call barLayer.setAggregateLabelFormat("{value|,.}")

Set lineLayer = c.addLineLayer(data1, &H66bbbb)
Call lineLayer.setUseYAxis2()
Call lineLayer.setDataLabelFormat("{value|,.}")
Call lineLayer.moveFront()

Call c.yAxis().setLabelFormat("{value|,.}")
Call c.yAxis2().setLabelFormat("{value|,.}")

Does anyone know if it's possible to do something like this using this technology?

I think the description is enough to contextualize

1

There are 1 best solutions below

0
On

You can use Layer.addCustomAggregateLabel, Layer.addCustomDataLabel and Axis.addLabel to completely control the labels. You can even use CDML to apply different colors to the labels.

Set cd = CreateObject("ChartDirector.API")

Set c = cd.XYChart(800, 440)
Call c.setPlotArea(30, 30, 740, 360)
Call c.xAxis().setLabels(labels)
Set barLayer = c.addBarLayer(data0, &H444466)

For i = 0 to UBound(data0)
    label = c.formatValue(Abs(data0(i)), "{value|,.}")
    If data0(i) < 0 Then label = "(" & label & ")"
    Call layer.addCustomAggregateLabel(i, label, "Arial Bold", 10)
Next

Set lineLayer = c.addLineLayer(data1, &H66bbbb)
Call lineLayer.setUseYAxis2()
Call lineLayer.setDataLabelFormat("{value|,.}")
Call lineLayer.moveFront()

For i = 0 to UBound(data1)
    label = c.formatValue(Abs(data1(i)), "{value|,.}")
    If data1(i) < 0 Then label = "(" & label & ")"
    Call layer.addCustomAggregateLabel(i, label, "Arial Bold", 10)
Next

Call c.layoutAxes()

ticks = c.yAxis().getTicks()
For i = 0 To Ubound(ticks)
    label = c.formatValue(Abs(ticks(i)), "{value|,.}")
    If ticks(i) < 0 Then label = "(" & label & ")"
    Call c.yAxis().addLabel(ticks(i), label)
Next

ticks = c.yAxis2().getTicks()
For i = 0 To Ubound(ticks)
    label = c.formatValue(Abs(ticks(i)), "{value|,.}")
    If ticks(i) < 0 Then label = "(" & label & ")"
    Call c.yAxis2().addLabel(ticks(i), label)
Next

Regards Peter Kwan