首页 > 解决方案 > C# - 如何绘制具有 3 个变量的图表(Position[32]、ByteValue[255]、Frequency[int])

问题描述

我想绘制一个图表,显示每个位置中出现的字节值的频率。

//位置[32][结果[255][频率[int]

Dictionary<UInt16, Dictionary<UInt16, int>>// 我想画这个

标签: c#graphchartsfrequency

解决方案


在此处输入图像描述

MSChart 在这方面不是很擅长。

但是,如果您的某个维度只能处理几个不同的值,您可以使用其 3D 选项并为每个 z 值添加一个系列。

我假设您只有 32 个位置,并选择此维度作为 z 轴。

MSChart 并没有真正的 z 轴属性。但是如果为 a 启用了 3D,ChartArea则将Series其用作第 3 维。所以我添加了32系列..

然后我循环遍历 256 个值并或多或少地随机创建一个频率值。

以下是上述结果的代码:

private void button1_Click(object sender, EventArgs e)
{
    chart1.Series.Clear();
    SeriesChartType type = SeriesChartType.Point;
    Random rnd = new Random(1);

    ChartArea ca = chart1.ChartAreas[0];

    ca.Area3DStyle.Enable3D = true;
    ca.Area3DStyle.PointGapDepth = 500;
    ca.Area3DStyle.PointDepth = 500;

    for (int p = 0; p < 32; p++)
    {
        Series s = chart1.Series.Add("P" + p);
        s.ChartType = type;

        for (int v = 0; v < 256; v++)
        {
            // test data
            int f = 25+(int)(rnd.Next(5) + 10*Math.Sin((p * (256 - v )/ 100f)));
            s.Points.AddXY(v, f);
        }
    }
}

为了旋转,我对所有按钮使用了这个常见的点击事件:

private void btn_rotate_Click(object sender, EventArgs e)
{
    int step = 10;
    ChartArea ca = chart1.ChartAreas[0];
    if (sender == btn_reset) { ca.Area3DStyle.Rotation = 30;  ca.Area3DStyle.Inclination = 30; } 
    if (sender == btn_left && ca.Area3DStyle.Rotation < 180-step) ca.Area3DStyle.Rotation+= step;
    if (sender == btn_right && ca.Area3DStyle.Rotation > -180-step) ca.Area3DStyle.Rotation-= step;
    if (sender == btn_down && ca.Area3DStyle.Inclination < 90-step) ca.Area3DStyle.Inclination+= step;
    if (sender == btn_up && ca.Area3DStyle.Inclination > -90-step) ca.Area3DStyle.Inclination-= step;
}

推荐阅读