在C#中绘制非数值变量的图表

时间:2014-12-11 22:46:50

标签: c# .net charts

我的Windows窗体应用程序项目中存在图表绘制问题。

我想画一个这样的图表: http://upload7.ir/imgs/2014-12/84930837744513480976.jpg

X轴和Y轴上的单词是相关的,彩色矩形中的字母是每个关系的值。我想要一张这样的图表。 谁能帮我画那样的东西? 我要感恩。

问候。

2 个答案:

答案 0 :(得分:0)

你的问题很模糊,但如果你想要那个图表,这是否意味着你不需要这些值是动态的?如果是这样,最简单的解决方案是将其保存为图像并在您需要的应​​用程序中显示图像。显然,如果您需要动态的值或颜色,这将无法工作。

答案 1 :(得分:0)

这是一个类AreaChart,它绘制的图表就像你展示的那样。

这是它的外观,内置值:

AreaChart

将类添加到项目中并进行编译。它将显示在工具箱中,您可以将其放在表单上。 (在你这样做之前备份你的项目!)

您可以在设计器或代码中设置多个值,包括尺寸BackColor, Font, and Labels ..使用提供的方法在代码中设置颜色和文本!

using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

class AreaChart : Panel
{
     [Category("Appearance"), Description("Padding around the ChartArea")]
    public Padding ChartPadding { get; set; }
     [Category("Appearance"), 
              Description("Axixs Origin offsets from Bottom Left of the Chart")]
    public Point AxisOriginOffset { get; set; }
     [Category("Appearance"),Description("Number of Rows")]
    public int RowNum { get; set; }
     [Category("Appearance"), Description("Number of Columns")]
    public int ColNum { get; set; }

     [Category("Appearance"), Description("Labeltexts for Y-Axis")]
    public string[] labelsY { get; set; }
     [Category("Appearance"), Description("Labeltexts for X-Axis")]
    public string[] labelsX { get; set; }

    Color[][] colors { get; set; }
    string[][] texts { get; set; }

    Rectangle chartArea = Rectangle.Empty;
    Point axisOrigin = Point.Empty


    public void Init()
    {
        chartArea = new Rectangle(ChartPadding.Left, ChartPadding.Top,
                    this.Width - ChartPadding.Left - ChartPadding.Right, 
                    this.Height - ChartPadding.Top - ChartPadding.Bottom);

        axisOrigin = new Point(AxisOriginOffset.X, this.Height - AxisOriginOffset.Y);

        colors = new Color[RowNum][];
        for (int r = 0; r < RowNum; r++) colors[r] = new Color[ColNum];
        texts = new string[RowNum][];
        for (int r = 0; r < RowNum; r++) texts[r] = new string[ColNum];
        labelsX = new string[ColNum];  //*
        labelsY = new string[RowNum];  //*
    }


    public AreaChart()
    {
        ChartPadding = new Padding(80, 40, 40, 40);
        AxisOriginOffset = new Point(60, 20);
        RowNum = 3;
        ColNum = 2;
        BackColor = Color.AntiqueWhite;

        Init();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (this.DesignMode) Init();      // make the designer show the current
        if (this.DesignMode) InitDemo();  // ...sizes, colors and texts

        int x = chartArea.X;
        int y = chartArea.Y;
        SizeF tSize = e.Graphics.MeasureString("XX", this.Font, 9999);
        int th = (int)tSize.Height / 2;
        int tw = (int)tSize.Width / 2;
        int h = chartArea.Height / RowNum;
        int w = chartArea.Width / ColNum; e.Graphics.Clear(BackColor);

        e.Graphics.DrawLine(Pens.Black, axisOrigin, 
                            new Point(axisOrigin.X, chartArea.Top));
        e.Graphics.DrawLine(Pens.Black, axisOrigin, 
                            new Point( chartArea.Right, axisOrigin.Y));

        for (int r = 0; r < RowNum; r++)
            for (int c = 0; c < ColNum; c++)
            {
                e.Graphics.FillRectangle(new SolidBrush(colors[r][c]), 
                  x + c * w, y + r * h, w, h);
                e.Graphics.DrawRectangle(Pens.Black, x + c * w, y + r * h, w, h);
                e.Graphics.DrawString(texts[r][c], this.Font, Brushes.Black, 
                  x + c * w  + w / 2 - tw, y + r * h + h / 2 - th);

            }
        for (int r = 0; r < RowNum; r++)
            e.Graphics.DrawString(labelsY[r], this.Font, Brushes.Black, 
              AxisOriginOffset.X - tw * 2, y + r * h + h / 2 - th);  //*

        for (int c = 0; c < ColNum; c++)
            e.Graphics.DrawString(labelsX[c], this.Font, Brushes.Black,
                x + c * w + w / 2 - tw, axisOrigin.Y );             //*

        base.OnPaint(e);;
    }

    public void setColor (int row, int col, Color color)
    {
        try
        {
            colors[row][col] = color;
        } catch { throw new Exception("setColor : array index out of bounds!"); }
    }

    public void setText(int row, int col, string text)
    {
        try
        {
            texts[row][col] = text;
        } catch { throw new Exception("setText: array index out of bounds!"); }
    }

    public void setLabelX(int col, string text)      //*
    {
        try
        {
            labelsX[col] = text;
        } catch { throw new Exception("array index out of bounds!"); }
    }

    public void setLabelY(int row, string text)      //*
    {
        try
        {
            labelsY[row] = text;
        } catch { throw new Exception("array index out of bounds!"); }
    }

    public void InitDemo()
    {
        setColor(0, 0, Color.Plum);
        setColor(1, 0, Color.GreenYellow);
        setColor(2, 0, Color.Gold);
        setColor(0, 1, Color.LightSkyBlue);
        setColor(1, 1, Color.NavajoWhite);
        setColor(2, 1, Color.Pink);

        setText(0, 0, "AA");
        setText(1, 0, "BA");
        setText(2, 0, "CA");
        setText(0, 1, "AB");
        setText(1, 1, "BB");
        setText(2, 1, "BC");

        setLabelY(0, "A");     //*
        setLabelY(1, "B");     //*
        setLabelY(2, "C");     //*

        setLabelX(0, "A");     //*
        setLabelX(1, "B");     //*
    }


}

在您的表格Initializecomponent()之后,您应该致电

areaChart1.Init();
areaChart1.InitDemo();

显示我显示的演示图表。要更改它,请使用以下调用:

areaChart1.ColNum = 3;
areaChart1.Init();
areaChart1.InitDemo();
areaChart1.setColor(0, 2, Color.Yellow);
areaChart1.setLabelY(2, "ZZ");
//..