Domanda

I have a pie chart I created using mschart and I, for now, have hard-coded the data populated. When selected a slice, I want a label to appear displaying the value - it does this now BUT aswell as displaying on the chart (correct), it displays in the series (incorrect).

Pseudo Code

//Diplay chart and data
//Highlight over slices and get #PERCENT (also when selected slice)
//Select slice
//Expand slice
//Display #VAL on slice & NOT in Series
//Re-select slice
//Minimise slice

I have two methods - MouseDown and MouseMove: (The #PERCENT tooltip works fine)

private void PieChart_MouseDown(object sender, MouseEventArgs e)
{
    HitTestResult result = PieChart.HitTest(e.X, e.Y);

    // Exit event if no item was clicked on
    if (result.PointIndex < 0)
    {
        return;
    }

    // Check if data point is already exploded
    bool exploded = (PieChart.Series[0].Points[result.PointIndex].CustomProperties == "Exploded=true");

    // Remove all exploded attributes
    foreach (DataPoint p in PieChart.Series[0].Points)
    {
        p.CustomProperties = "PieLabelStyle = Disabled, Exploded = False";
        p.Label = "";
        p.ToolTip = "";
    }

    // If data point is already exploded get out
    if (exploded)
    {
        return;
    }

    // If data point is selected or if legend item is selected
    if (result.ChartElementType == ChartElementType.DataPoint || result.ChartElementType == ChartElementType.LegendItem)
    { 
        DataPoint point = PieChart.Series[0].Points[result.PointIndex];
        point.Label = "Value: #VAL";
        point.LabelBackColor = Color.Bisque;
        point.LabelBorderColor = Color.Black;
        point.Font = new Font("Calibri Light", 8);
        point.CustomProperties = "PieLabelStyle = Inside, Exploded = True";
    }
}

private void PieChart_MouseMove(object sender, MouseEventArgs e)
{
    HitTestResult result = PieChart.HitTest(e.X, e.Y);

    foreach (DataPoint point in PieChart.Series[0].Points) // ALL DATA POINTS - SECTIONS OF CHART
    {
        point.BackSecondaryColor = Color.Black;
        point.BackHatchStyle = ChartHatchStyle.None;
        point.BorderWidth = 1;
    }

    // If a Data Point or a Legend item is selected
    if (result.ChartElementType == ChartElementType.DataPoint || result.ChartElementType == ChartElementType.LegendItem)
    {
        this.Cursor = Cursors.Hand;
        DataPoint point = PieChart.Series[0].Points[result.PointIndex];
        point.ToolTip = "Percentage: #PERCENT";
        point.BackSecondaryColor = Color.White;
        point.BackHatchStyle = ChartHatchStyle.Percent25;
        point.BorderWidth = 2;
    }
    else
    {
        this.Cursor = Cursors.Default;
    }
}

What it does now:-

enter image description here

What I want it to do:-

Notice series7 is now visible and NOT the value - CORRECT! enter image description here

Attempted solutions:

PieChart.Series[0].Points.Add(new DataPoint(1, 1) { LegendText = "aaa" });

enter image description here

point.Label = "Value: #VAL";
point.LegendText = "Series#INDEX";

enter image description here

È stato utile?

Soluzione

Simply use DataPoint's LegendText property when populating chart's data:

PieChart.Series[0].Points.Add(new DataPoint(1, 1) { LegendText = "aaa" });
PieChart.Series[0].Points.Add(new DataPoint(1, 2) { LegendText = "bbb" });
PieChart.Series[0].Points.Add(new DataPoint(1, 3) { LegendText = "ccc" });

Alternative way is to set LegendText each time you set Label:

point.Label = "Value: #VAL";
point.LegendText = "Series#INDEX"; // Or whatever you want
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top