现在的位置: 首页 > 综合 > 正文

MSChart 设置饼图颜色 图例背景色 图例显示位置

2013年04月01日 ⁄ 综合 ⁄ 共 3362字 ⁄ 字号 评论关闭

 

   chartField.Series.Clear();
            chartField.ChartAreas.Clear();
            chartField.Legends.Clear();
            chartField.ChartAreas.Add(new ChartArea("Default"));

//设置图例背景色

            Legend leg = new Legend();
            leg.BackColor = Color.Transparent;
            leg.Name = "Legend1";
            chartField.Legends.Add(leg);
            chartField.BackColor = Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240)))));
            chartField.BackGradientStyle = GradientStyle.TopBottom;
            chartField.BackSecondaryColor = Color.White;
        

            foreach (string key in chartLineDic.Keys)
            {
                Series serie = new Series();
                serie.Name = key;
                serie.LegendText = key;
                serie.BorderWidth = 1;
                serie.ChartType = SeriesChartType.Line;
                serie.YValueType = ChartValueType.Int32;
                serie.XValueType = ChartValueType.DateTime;
                chartField.Series.Add(serie);
                foreach (Temp t in chartLineDic[key])
                {
                    chartField.Series[key].Points.AddXY(t.Date, t.Source);
                }
                chartField.Series[key].IsValueShownAsLabel = true;
                chartField.Series[key].LabelAngle = 10;
            }

            this.chartEventPie.Series.Clear();
            chartEventPie.ChartAreas.Clear();
            chartEventPie.Legends.Clear();
            chartEventPie.ChartAreas.Add(new ChartArea("Default"));
            chartEventPie.ChartAreas["Default"].BackColor = chartEventPie.BackColor;
            Legend legend = new Legend();
            Series series = new Series();
            legend.BackColor = chartEventPie.BackColor;
            legend.IsEquallySpacedItems = true;
            legend.IsTextAutoFit = false;
            legend.Name = "Default";
            this.chartEventPie.IsSoftShadows = false;

            if (chartEventPieDic != null)
            {
                int index = 0;
                foreach (string key in chartEventPieDic.Keys)
                {
                    DataPoint dataPoint = new DataPoint(0, chartEventPieDic[key]);
                    dataPoint.CustomProperties = "OriginalPointIndex=" + index;
                    dataPoint.LegendText = key;
                    dataPoint.IsValueShownAsLabel = false;

                    if (key == "提示")
                    {

//设置饼图颜色
                        dataPoint.Color = Color.Blue;
                    }
                    if (key == "报警")
                    {
                        dataPoint.Color = Color.Red;
                    }
                    if (key == "正常")
                    {
                        dataPoint.Color = Color.Green;
                    }
                    if (key == "预警")
                    {
                        dataPoint.Color = Color.Yellow;
                    }
                    series.Points.Add(dataPoint);
                    index++;
                }

                this.chartEventPie.Legends.Add(legend);
                series.ChartArea = "Default";
                series.ChartType = SeriesChartType.Pie;
                series.Label = "#PERCENT{P1}";
                series.Legend = "Default";
                series.XValueType = ChartValueType.Double;
                series.YValueType = ChartValueType.Double;
                this.chartEventPie.Series.Add(series);
                chartEventPie.Series[0]["CollectedColor"] = "Gray";
            }

图例显示位置

  legend.Alignment = StringAlignment.Center;
                legend.Docking = Docking.Bottom;

 

抱歉!评论已关闭.