饼图透明度,多个饼 [英] Pie Chart transparency, multiple Pies

查看:84
本文介绍了饼图透明度,多个饼的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想彼此叠放两个饼形图.然后,此处的想法是使其中一个更小,以便您只能看到后面的饼形图的外圈.我尝试使用:

chartCalProgres.BackColor = Color.Transparent;
chartCalProgres.ChartAreas[0].BackColor = Color.Transparent;

但是正如您在下面的链接中看到的那样,它没有用.任何人都知道如何实现这种效果?

饼图

应该像这样

解决方案

这对于饼图是不可能的,但是您可以通过ChartType.Doughnut很好地完成它:

以下是步骤:

1我们需要有两个Series和两个ChartAreas

2我们需要控制CA的位置,大小和InnerPlotPosition.它们必须覆盖并具有正确的尺寸.

3我们还需要控制我们使用的两个系列的DoughnutRadius.这是内半径.

4最后,我们需要将内部系列"的背景色"设置为透明".

以下是设置我的示例的代码:

    using System.Windows.Forms.DataVisualization.Charting;
    //..
    chart1.Series.Clear();
    Series S1 = chart1.Series.Add("Pie1");
    Series S2 = chart1.Series.Add("Pie2");

    chart1.ChartAreas.Clear();

    ChartArea CA1 = chart1.ChartAreas.Add("Outer");
    ChartArea CA2 = chart1.ChartAreas.Add("Inner");

    CA1.Position = new ElementPosition(0, 0, 100, 100);
    CA2.Position = new ElementPosition(0, 0, 100, 100);

    float innerSize = 60;
    float outerSize = 100;
    float baseDoughnutWidth = 25;

    CA1.InnerPlotPosition = new ElementPosition((100 - outerSize) / 2,
            (100 - outerSize) / 2 + 10, outerSize, outerSize - 10);

    CA2.InnerPlotPosition = new ElementPosition((100 - innerSize) / 2,
            (100 - innerSize) / 2 + 10, innerSize, innerSize - 10);

    S1["DoughnutRadius"] = 
     Math.Min(baseDoughnutWidth * (100 / outerSize), 99).ToString().Replace(",", ".");
    S2["DoughnutRadius"] = 
     Math.Min(baseDoughnutWidth * (100 / innerSize), 99).ToString().Replace(",", ".");


    S1.ChartArea = CA1.Name;
    S2.ChartArea = CA2.Name;

    S1.ChartType = SeriesChartType.Doughnut;
    S2.ChartType = SeriesChartType.Doughnut;

    CA2.BackColor = Color.Transparent;
    S1["DoughnutRadius"] = "41"; // leave just a little space!
    S2["DoughnutRadius"] = "99"; // 99 is the limit. a tiny spot remains open

    // test data, optional, R is a Random instance
    for (int i = 0; i < 7; i++)
    {
        S1.Points.AddXY(i, 42 - R.Next(44));
        S2.Points.AddXY(i, 77 - R.Next(88));
    }
}

请注意设置DoughnutRadius的奇怪方法;还请注意,图表中的许多数字都是图表控件大小的百分比.

我找到了代码Pie Charts

Should look like this

解决方案

This is not possible with Pie charts, but you can get it done rather nicely with the ChartType.Doughnut:

Here are the steps:

1 We need to have two Series and two ChartAreas

2 We need to control the Position, the Size and the InnerPlotPosition of the CAs. They must overlay and have the right sizes..

3 We also need to control the DoughnutRadius of the two Series we use. This is the inner radius.

4 Finally we need to set the Backcolor of the inner Series to Transparent.

Here is the code that set up my example:

    using System.Windows.Forms.DataVisualization.Charting;
    //..
    chart1.Series.Clear();
    Series S1 = chart1.Series.Add("Pie1");
    Series S2 = chart1.Series.Add("Pie2");

    chart1.ChartAreas.Clear();

    ChartArea CA1 = chart1.ChartAreas.Add("Outer");
    ChartArea CA2 = chart1.ChartAreas.Add("Inner");

    CA1.Position = new ElementPosition(0, 0, 100, 100);
    CA2.Position = new ElementPosition(0, 0, 100, 100);

    float innerSize = 60;
    float outerSize = 100;
    float baseDoughnutWidth = 25;

    CA1.InnerPlotPosition = new ElementPosition((100 - outerSize) / 2,
            (100 - outerSize) / 2 + 10, outerSize, outerSize - 10);

    CA2.InnerPlotPosition = new ElementPosition((100 - innerSize) / 2,
            (100 - innerSize) / 2 + 10, innerSize, innerSize - 10);

    S1["DoughnutRadius"] = 
     Math.Min(baseDoughnutWidth * (100 / outerSize), 99).ToString().Replace(",", ".");
    S2["DoughnutRadius"] = 
     Math.Min(baseDoughnutWidth * (100 / innerSize), 99).ToString().Replace(",", ".");


    S1.ChartArea = CA1.Name;
    S2.ChartArea = CA2.Name;

    S1.ChartType = SeriesChartType.Doughnut;
    S2.ChartType = SeriesChartType.Doughnut;

    CA2.BackColor = Color.Transparent;
    S1["DoughnutRadius"] = "41"; // leave just a little space!
    S2["DoughnutRadius"] = "99"; // 99 is the limit. a tiny spot remains open

    // test data, optional, R is a Random instance
    for (int i = 0; i < 7; i++)
    {
        S1.Points.AddXY(i, 42 - R.Next(44));
        S2.Points.AddXY(i, 77 - R.Next(88));
    }
}

Note the strange way to set the DoughnutRadius; also note that many numbers inside a chart are percentages of the chart controls sizes..!

I found the code here, all kudos to fireblade123, Escener Technologies!!

这篇关于饼图透明度,多个饼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆