c# - ASP.NET 图表控件,是否可以在饼图上使用渐变?

标签 c# asp.net asp.net-charts

我正在使用 ASP.NET 图表控件制作图表原型(prototype)。我有以下代码来生成我的图表。我正在努力尽可能接近客户的品牌准则。品牌指南在饼图部分中使用渐变。使用自定义颜色时可以吗?

    /// <summary>
    /// Create an image of a chart from the given data
    /// </summary>
    /// <param name="data">Dictionary of data, labels as key and value as value.</param>
    /// <returns>The bytes of an image</returns>
    private Byte[] CreatePieChart(Dictionary<string,string> data)
    {
        //Set up the chart
        var chart = new Chart
        {
            Width = 550,
            Height = 400,
            RenderType = RenderType.BinaryStreaming,
            AntiAliasing = AntiAliasingStyles.All,
            TextAntiAliasingQuality = TextAntiAliasingQuality.High
        };

        //Add the title
        chart.Titles.Add("Chart 1");
        chart.Titles[0].Font = new Font("Arial", 16f);

        //Set up labels etc
        chart.ChartAreas.Add("");
        chart.ChartAreas[0].AxisX.TitleFont = new Font("Arial", 12f);
        chart.ChartAreas[0].AxisY.TitleFont = new Font("Arial", 12f);
        chart.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Arial", 10f);
        chart.ChartAreas[0].AxisX.LabelStyle.Angle = -90;
        chart.ChartAreas[0].BackColor = Color.White;

        //Set up the series and specify Pie chart
        chart.Series.Add("");
        chart.Series[0].ChartType = SeriesChartType.Pie;
        chart.Series[0].SetCustomProperty("PieLabelStyle", "outside");
        chart.Series[0].IsValueShownAsLabel = true;
        chart.Series[0].BackGradientStyle = GradientStyle.Center;

        //MAke the chart 3D
        chart.ChartAreas[0].Area3DStyle.Enable3D = true;
        //chart.ChartAreas[0].Area3DStyle.Perspective = 75;
        chart.ChartAreas[0].Area3DStyle.Inclination = 0;

        //Loop over the data and add it to the series
        foreach (var item in data)
        {
            chart.Series[0].Points.AddXY(item.Key, Convert.ToDouble(item.Value));
        }

        //Add a legend
        chart.Legends.Add("");
        chart.Legends[0].InsideChartArea = "";

        Color[] myPalette = new Color[6]{ 
             Color.FromArgb(255,101,187,226), 
             Color.FromArgb(255,253,214,91), 
             Color.FromArgb(255,38,190,151), 
             Color.FromArgb(255,253,183,101),
             Color.FromArgb(255,218,143,183),
            Color.FromArgb(255,242,242,242)};

        chart.Palette = ChartColorPalette.None;
        chart.PaletteCustomColors = myPalette;

        byte[] chartBytes;

        //Write the chart image to a stream and get the bytes
        using (var chartimage = new MemoryStream())
        {
            chart.SaveImage(chartimage, ChartImageFormat.Png);
            chartBytes = chartimage.GetBuffer();
        }

        return chartBytes;
    }

最佳答案

可以使用 ASP.NET 图表在饼图上获得渐变。

以下是相关行:

        Color[] myPalette = new Color[5]{ 
             Color.FromArgb(255,101,187,226), 
             Color.FromArgb(255,253,214,91), 
             Color.FromArgb(255,38,190,151), 
             Color.FromArgb(255,253,183,101),
             Color.FromArgb(255,218,143,183)};

        chart.Palette = ChartColorPalette.None;
        chart.PaletteCustomColors = myPalette;

        //Loop over the data and add it to the series
        int i = 0;
        foreach (var item in data)
        {
            chart.Series[0].Points.AddXY(item.Key, Convert.ToDouble(item.Value));
            chart.Series[0].Points[i].BackGradientStyle = GradientStyle.Center;
            chart.Series[0].Points[i].Color = myPalette[i];
            chart.Series[0].Points[i].BackSecondaryColor = LightenColor(myPalette[i]);
            //chart.Series[0].Points[i].SetCustomProperty("Exploded","true");

            i++;
        }

原型(prototype)代码。产生尚可的渐变。

关于c# - ASP.NET 图表控件,是否可以在饼图上使用渐变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7513058/

相关文章:

c# - 如何更新 linq2db 中的两列?

c# - 如何在 C# 中使 Dropdownlist 只读

asp.net - 如何使我的 asp 图表在 x 方向上可滚动

c# - 如何获取字符串数组列表的最后一个元素?

c# - 序列化/反序列化后使用的ram大小异常增加

c# - 创建新的空白应用程序时,Xamarin Android Visual Studio布局未呈现

c# - ASP.NET c# 中 System.Threading.Timer 和 Thread.Sleep() 的区别

c# - 使用 Asp.Net 赢得应用商店

vb.net - ASP.NET 图表控件 - 我如何创建此条形图?