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

Message ListMessage List     Post MessagePost Message

  Color or lables in a rose
Posted by Alain on Feb-14-2022 21:36
Attachments:
Hello,

I would like to print the text of the labels of the different axes of a rose in the same color as the axe itself.
is it possible ? How do I have to proceed ?

Here is an example :
#include "chartdir.h"

int main(int argc, char *argv[])
{
    // Data for the chart
    double data[] = {1.19, 0.06, 0.8, 1.42, 2.13, 1.77, 1.22, 3.0, 1.5,
                        0.86, 0.95, 0.63, 0.12, 0.15, 1.6, 2.0, 1.4, 0.70};

    int colors[] = {0x2E972E, 0xFF1717, 0xFF1717, 0x463AD0, 0x83345B, 0x521678, 0x345320, 0x973400, 0xB10F28,
                0x2E972E, 0xFF1717, 0xFF1717, 0x463AD0, 0x83345B, 0x521678, 0x345320, 0x973400, 0xB10F28};

    // The labels on the angular axis (spokes)
    const char* labels[] = {
"My label01",
"My label02",
"My label03",
"My label04",
"My label05",
"My label06",
"My label07",
"My label08",
"My label09",
"My label10",
"My label11",
"My label12",
"My label13",
"My label14",
"My label15",
"My label16",
"My label18",
"My label19"
};

    const int labels_size = (int)(sizeof(labels)/sizeof(*labels));

    PolarChart *c = new PolarChart(800, 600, Chart::silverColor(), 0x000000, 1);

    c->addTitle("Votre situation", "arialbi.ttf", 15, 0xffffff)->setBackground(0x000000);
    c->setPlotArea(400, 320, 180, 0xffffff);
    c->setGridStyle(false);

    // Set angular axis as 0 - 360, with a spoke every 20 units
    c->angularAxis()->setLinearScale(0, 360, 20);

    // Set angular axis as 0 - 360, either 8 spokes
    c->angularAxis()->setLinearScale(0, 360, StringArray(labels, labels_size));

    // Add sectors to the chart as sector zones
    for(int i = 0; i < (int)(sizeof(data) / sizeof(data[0])); ++i) {
        c->angularAxis()->addZone(20*i-90+1, 20*i-70-1, 0, data[i], colors[i], colors[i]);
    }

    c->addLineLayer(DoubleArray(data, (int)(sizeof(data) / sizeof(data[0]))), Chart::Transparent);

    // Output the chart
    c->makeChart("roseidx.png");
    c->makeChart("roseidx.svg");

    delete c;
    return 0;
}

How can I use the colors defined in the colors array to print the labels ?

Thank you for your help!

Regards

Alain
roseidx.png

  Re: Color or lables in a rose
Posted by Peter Kwan on Feb-15-2022 13:39
Hi Alain,

You can use CDML to color the labels. For example, you can change the "My label01" to "<*color=521678*>My label01".

The following is an example that can generate the text strings from your labels array:

// put in the header section of your code
#include <string>
#include <vector>
#include <sstream>

// change "My labelXX" to  "<*color=XXXXXX*>My labelXX"
std::vector<std::string> buffer(labels_size);
for (int i = 0; i < labels_size; ++i) {
    buffer[i] = ((std::ostringstream() << "<*color=" << std::hex << colors[(i + labels_size - 5) % labels_size] << "*>" << labels[i]).str());
    labels[i] = buffer[i].c_str();
}

(Note: the color array is indexed with [(i + labels_size - 5)  % label_size] to match the way your code colors the sectors.

Hope this can help.

Regards
Peter Kwan

  Re: Color or lables in a rose
Posted by Alain on Feb-15-2022 23:15
Hi Peter,

Thank you for your great support!
That is exactly what I needed. I am always surprised by the possibilities of ChartDirector.

Regards,

Alain