Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言绘制非数值变量图表#_C#_.net_Charts - Fatal编程技术网

C# 用C语言绘制非数值变量图表#

C# 用C语言绘制非数值变量图表#,c#,.net,charts,C#,.net,Charts,我的Windows窗体应用程序项目中的图表绘制有问题 我想画一张这样的图表: X轴和Y轴中的单词是相关的,彩色矩形中的字母是每个关系的值。我想要一张像这样的图表。 有人能帮我画那样的东西吗? 我会很感激的 问候。你的问题很模糊,但如果你想要那张图表,这是否意味着你不需要这些值是动态的?如果是这样,最简单的解决方案不是将其保存为图像并在应用程序中需要的位置显示图像吗。显然,如果您需要动态的值或颜色,这将不起作用。这里有一个类AreaChart,它绘制了一个与您显示的类似的图表 下面是它的外观,带

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

我想画一张这样的图表:

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


问候。

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

这里有一个类
AreaChart
,它绘制了一个与您显示的类似的图表

下面是它的外观,带有内置值:

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

您可以在设计器或代码中设置许多值,包括尺寸、
背景色、字体和标签。。使用提供的方法设置代码中的颜色和文本

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");     //*
    }


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

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

到目前为止你试过什么?那它不起作用呢?你的问题太宽泛了。钢笔和一张纸?你可以用两种方法来做:或者用图形的方法画东西,:DrawLine,DrawRectangle,FillRectangle和DrawString也可以使用图表控件。她说你可以在代码中完成,但我建议使用VS设计器。。我不知道你真正想做什么,我不能提供建议。但是为了快速的结果和完全的控制,我想我会自己画。我在我的项目中画了很多图表,和图表课一起工作。但我找不到这种图表的匹配类型。如果您熟悉模糊系统,此图表显示了一个简单的2d知识库。我希望你们能在这个项目中救我一命!提交时间是2天后。我已经更新了一点代码。不!他们是动态的!!!我所需要的就是这样的图表格式!我以为这对C#dudes来说是个简单的任务!谢谢它非常有用,但也有一些问题。1-如何管理分组框上图表的位置和大小。2-我想在图表的X轴上设置另一个标签。我的时间不多了,请帮帮我。强烈期待您的反馈。1-??如果您已将图表放置在groupbox中,您可以用鼠标移动图表并调整其大小?没有你说得对,我忘了。请参阅(//*)更新的答案!不,我把图表放在分组框中,它的大小非常小(即使我的分组框很大)。我试图改变它的大小,但只有图表的背景变化和图表的大小保持不变。我使用这个:AreaChart a1=新的AreaChart()。a1.尺寸=新系统图纸尺寸(500250);我知道,这是设计师的展示问题;不确定我什么时候有时间修理。-不过,这只是在设计中。在运行时,它将填充其区域@Ali Crash:找到了一个修复方法:如果添加OnPaint代码的前两行,图表将正确显示在设计器中!
areaChart1.ColNum = 3;
areaChart1.Init();
areaChart1.InitDemo();
areaChart1.setColor(0, 2, Color.Yellow);
areaChart1.setLabelY(2, "ZZ");
//..