[c#]#VAL 视图标签在饼图上内外不系列切片选择时 = true

发布时间: 2017/2/23 0:06:38
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个用 mschart 的饼图和我,现在,有硬编码的数据填充。当选择了一片,我想要一个标签显示显示值 — — 这一点现在以及显示图表(正确),它会显示在系列(不正确)

伪代码

//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

我有两种方法- MouseDownMouseMove: (#PERCENT 工具提示工作正常)

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;
    }
}

它现在的样子:-

enter image description here

我想要它做的事情:-

通知 series7 现在是可见和不值-正确 ! enter image description here

尝试的解决办法︰

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

解决方法 1:

只需使用数据点的 LegendText 属性填充图表的数据时︰

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" });

替代方法是设置 LegendText 每次你设置 Label :

point.Label = "Value: #VAL";
point.LegendText = "Series#INDEX"; // Or whatever you want
赞助商