MSChart饼图重叠标签问题

时间:2016-01-04 14:05:13

标签: c#

在针对给定问题寻找合适的解决方案后,我无法接受论坛中的解决方案,而且微软提供的解决方案也不是我想要的。

以下是问题的一个示例:

(Link to image) MSChart pie diagram with overlapping labels

我尝试在图表外部设置标签,但首先,连接线有点难看,其次,外部标签使用太多空间。 SmartLabels也没有解决问题。

编辑: 有人知道如何设置标签半径吗?看起来每个标签的中心是一个固定的半径?!

有没有人弄清楚如何修复重叠标签?

1 个答案:

答案 0 :(得分:0)

准备您的数据!

您必须在系列中设置点,因此最初可以轻松创建列表,将数据输入到该列表,按照要显示的值的降序或升序对列表进行排序,然后选择该列表将最小值放在两个最大值之间。

例如:

100 70 50 三十 10 五 (旧订单)

100 五 70 10 50 三十 (新订单)

结果图:

(Link to image) MSChart pie diagram without overlapping labels

让我们可以使用值和工具提示文本调用饼图的每个切片。 所以你需要一个这样的课程:

private class CatValue
{
    public string CategoryName;
    public decimal Value;
    public string TooltipText;
}

现在将您的数据放入List对象中,例如:

List<CatValue> listOfPieSlices = new List<CatValue>();
// ....
// Add your data to the list object
// ....
// Sort the list in ascending order
listOfPieSlices.Sort((x, y) => x.Value.CompareTo(y.Value));
// Sort the list in descending order
// listOfPieSlices.Sort((x, y) => -x.Value.CompareTo(y.Value));
//
// Now sort the "listOfPieSlices" with the specialSort methode:
listOfPieSlices = specialSort<CatValue>(listOfPieSlices);

// Now create the Series of DataPoints
Series sortedPoints = new Series("PCDSsorted");
for (int i = 0; i < listOfPieSlices.Count; i++)
{
    sortedPoints.Points.AddY(Math.Abs(listOfPieSlices[i].Value));        
    sortedPoints.Points[i].Label = listOfPieSlices[i].CategoryName + ":  "+listOfPieSlices[i].Value.ToString();
    sortedPoints.Points[i].Font = new Font("Microsoft Sans Serif", 12);
    sortedPoints.Points[i].LabelToolTip = listOfPieSlices[i].Tooltip;
    sortedPoints.Points[i].ToolTip = listOfPieSlices[i].Tooltip;
}    

private List<T> specialSort<T>(List<T> objects)
{
    if (objects == null) return null;

    int count = objects.Count;
    List<T> sortedList = new List<T>();
    for (int i = 0; i <= count - 1; i += 2)
    {
        if(i == count - 1)
            sortedList.Add(objects[i / 2]);
        else
        {
            sortedList.Add(objects[i / 2]);
            sortedList.Add(objects[count - 1 - (i / 2)]);
        }
    }
    return sortedList;
}

希望这适用于那里的人,也适合我。

快乐编码