带有不同条形颜色的Zedgraph水平条形图C#

时间:2015-05-20 08:40:04

标签: c# colors zedgraph

我正在研究zedgraph并生成一个水平条形图。我只想知道是否有任何方法可以使每个单独的条形成不同的颜色。代码的输出是可以接受的,我只打算改变生成的条的颜色。任何帮助将非常感激。谢谢!

        myPane.YAxis.Title.Text = "Nominees";
        myPane.XAxis.Title.Text = "Votes";

        // Make up some random data points

        string[] labels= new string[count];

        for (int i = count-1; i >= 0; i--)
        {
            labels[counter] = voternames[i];
            counter++;
        }



        for (int i = count1-1; i >= 0; i--)
        {
            y0[counter1] = Convert.ToDouble(votes[i]);
            counter1++;
        }


        // Generate a red bar with "Curve 1" in the legend
        BarItem myBar = myPane.AddBar("", y0, null, Color.Green);


        // Draw the X tics between the labels instead of 
        // at the labels
        myPane.YAxis.MajorTic.IsBetweenLabels = false;
        // Set the XAxis labels
        myPane.YAxis.Scale.TextLabels = labels;
        // Set the XAxis to Text type
        myPane.YAxis.Type = AxisType.Text;

        myPane.BarSettings.Base = BarBase.Y;

        // Fill the Axis and Pane backgrounds
        myPane.Fill = new Fill(Color.FromArgb(250, 250, 255));

        // Tell ZedGraph to refigure the
        // axes since the data have changed

        zgc.AxisChange();
        zgc.Refresh();

1 个答案:

答案 0 :(得分:0)

找到我要找的东西,源的原始链接是: http://www.ironpython.info/index.php?title=Multi-colored_Bar_Chart_with_ZedGraph。主要的修改是制作一个Point对列表而不是发送双数组来创建条形图,提供颜色数组的引用和设置填充的最小和最大范围。修改后的代码如下:

        myPane.YAxis.Title.Text = "Nominees";
        myPane.XAxis.Title.Text = "Votes";

        // Make up some random data points

        string[] labels= new string[count];
        //string[] labelx = new string[count];
        for (int i = count-1; i >= 0; i--)
        {
            labels[counter] = voternames[i];
            counter++;
        }



        for (int i = count1-1; i >= 0; i--)
        {
            y0[counter1] = Convert.ToDouble(votes[i]);
            counter1++;
        }

        for (int i = 0; i < y0.Length; i++)
        {
    //Adding the x axis data and using y axis as a source to color a single bar
            list.Add(y0[i], i / 2.0);
        }


        Color[] colors = new Color[] {Color.Red, Color.Yellow, Color.Green, Color.Blue, Color.Purple};

    // Generate a bar with point pair list in the legend
        BarItem myBar = myPane.AddBar("", list, Color.Green);

    // Giving ref of color array
    myBar.Bar.Fill = new Fill(colors);

    //Setting to fill with using point values of y axis
        myBar.Bar.Fill.Type = FillType.GradientByY;

    //Setting min and max range is important
        myBar.Bar.Fill.RangeMin = 0;
        myBar.Bar.Fill.RangeMax = Convert.ToInt32(y0[0]);

        // Draw the X tics between the labels instead of 
        // at the labels
        myPane.YAxis.MajorTic.IsBetweenLabels = false;
        // Set the XAxis labels
        myPane.YAxis.Scale.TextLabels = labels;
        // Set the XAxis to Text type
        myPane.YAxis.Type = AxisType.Text;

        myPane.BarSettings.Base = BarBase.Y;

        // Fill the Axis and Pane backgrounds
        //myPane.Chart.Fill = new Fill(Color.White,Color.FromArgb(255, 255, 166), 90F);
        myPane.Fill = new Fill(Color.FromArgb(250, 250, 255));

        // Tell ZedGraph to refigure the
        // axes since the data have changed

        zgc.AxisChange();
        zgc.Refresh();