|
How to set label format per sector in a pie chart in C++ |
| Posted by choi-jihyeon on Jan-29-2026 15:03 |
|
Hello,
I am working with a pie chart in ChartDirector (C++ version), and I am trying to set the label format per sector.
I have the following code:
else if (data.GetDoubleArray().len > 0)
{
for (int idx = 0; idx < data.GetDoubleArray().len; idx++)
{
Sector* sector = pieChart.sector(idx);
if (data.GetDoubleArray()[idx] != 0)
{
if (m_pMarkArry == NULL ||
(m_pMarkArry != NULL &&
m_pMarkArry[0][idx] != NULL &&
m_pMarkArry[0][idx] != TCHARtoUTF8(_T(" "))))
{
sector->setLabelFormat(TCHARtoUTF8(info_chartLayout.m_label.data()));
}
else
{
sector->setLabelFormat(" ");
}
}
else
{
sector->setLabelFormat(" ");
}
}
}
When this code is executed, the label format is not applied per sector.
Instead, all sectors end up using the same label format value:
TCHARtoUTF8(info_chartLayout.m_label.data())
In other words, even though Sector.setLabelFormat() is called for each sector, the format appears to be applied globally rather than individually.
My questions are:
What is the correct way to set different label formats for each sector in a pie chart using the C++ version of ChartDirector?
If I want to hide the label for a specific sector, is using
sector->setLabelFormat(" ");
the correct and recommended approach?
Any clarification or guidance would be appreciated.
Thank you. |
Re: How to set label format per sector in a pie chart in C++ |
| Posted by Peter Kwan on Jan-29-2026 22:32 |
|
Hi choi-jihyeon,
I suspect the following line is incorrect:
m_pMarkArry[0][idx] != TCHARtoUTF8(_T(" "))))
You cannot compare two text string pointers using "!=" or "==". You need to use strcmp. It is like:
strcmp(m_pMarkArry[0][idx], " ") != 0
In your original code, the m_pMarkArry[0][idx] != TCHARtoUTF8(_T(" ")))) is likely to be false, because the pointers to the two strings are likely different. Your code will always choose the "else { sector->setLabelFormat(" "); }" for all sectors, so all sectors have the same format.
I have come up with a short example that demonstrates using setLabelFormat to set different formats for the labels:
// The data for the pie chart
double data[] = {25, 18, 15, 12, 8, 30, 35};
const int data_size = (int)(sizeof(data)/sizeof(*data));
// The labels for the pie chart
const char* labels[] = {"Labor", "Licenses", "Taxes", "Legal", "Insurance", "Facilities",
"Production"};
const int labels_size = (int)(sizeof(labels)/sizeof(*labels));
// Create a PieChart object of size 360 x 300 pixels
PieChart* c = new PieChart(360, 300);
// Set the center of the pie at (180, 140) and the radius to 100 pixels
c->setPieSize(180, 140, 100);
// Set the pie data and the pie labels
c->setData(DoubleArray(data, data_size), StringArray(labels, labels_size));
c->sector(0)->setLabelFormat(TCHARtoUTF8(_T("AAA BBB")));
c->sector(1)->setLabelFormat(TCHARtoUTF8(_T("CCC {value} DDD")));
c->sector(2)->setLabelFormat(TCHARtoUTF8(_T(" ")));
c->sector(3)->setLabelFormat(TCHARtoUTF8(_T("{percent}%")));
c->sector(4)->setLabelFormat(TCHARtoUTF8(_T("<*font=Arial Bold,color=000088,size=11*>{label}<*/font*><*br*>{percent|2}%")));
c->sector(5)->setLabelFormat(TCHARtoUTF8(_T("<*font=Times New Roman Italic,size=14,color=FF0000*>{label}<*/*>")));
c->makeChart("pietest.png");
Best Regards
Peter Kwan
|
|