C# 中的简单条形图

发布于 2024-12-15 14:36:25 字数 2665 浏览 0 评论 0 原文

我想问是否可以使用 MScharts 创建两个简单的条形图,如下所示:

Screen拍摄http://img16.imageshack.us/img16/4413/desktoptf.png

我想要上图的精确复制品,所以没有图表或网格,只是两个像这样的简单条形。

另一件事是,最大值由条形两端的红色小条纹指示,并且应该保留在那里,直到超过最大值。

我的想法是,我正在读取应该动态应用于条形图的实时数据(4 个值)。

那么有人知道该怎么做吗?我目前正在使用 MScharts 插件(或者使用 C# 的绘图功能而不是 MScharts 会更好吗?)。

提前致谢。

编辑:

好的,这就是我想出的:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool k = false;
        Random random = new Random();
        int max = 0;

        protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            int i = 30;
            Graphics gfx = paintEvnt.Graphics;
            Pen myPen = new Pen(Color.Black);

                for (i = 40; i < 640; i = i + 100)
                {
                    gfx.DrawLine(myPen, i, 25, i, 35); 
                }

                for (i = 40; i < 640; i = i + 100)
                {
                    gfx.DrawLine(myPen, i, 55, i, 65);
                }

                Color brushColor = Color.FromArgb(0, 0, 255);
                SolidBrush myBrush = new SolidBrush(brushColor);

                    int randomnumber = random.Next(0, 601);
                    gfx.FillRectangle(myBrush, 33, 33, randomnumber, 25);

                    if (randomnumber + 33 > max)
                    {
                        max = randomnumber + 33;
                        gfx.DrawLine(new Pen(Color.Red, 3), max, 30, max, 60);
                    }
                    else
                    {
                        gfx.DrawLine(new Pen(Color.Red, 3), max, 30, max, 60);
                    }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Invalidate();
        }
    }
}

这就是它的样子: http ://img411.imageshack.us/img411/5646/graphmj.jpg 每次按下按钮时,都会生成新的随机数据并覆盖旧的图表。然而,仍然存在一个问题。 红色指示器应该仅在超过最大值时增加,这就是我尝试在 OnPaint 方法中使用 if-query 实现的内容,但有时它仍然会组成随机值并完全关闭,超过旧值尽管新的随机值更低......但这没有意义。

这是该项目,以防有人想尝试并帮助我: http://up.k10x.net/ambgolrngulg/LevelMeter.zip

我是真的很无能,因为代码对我来说看起来是正确的。

I wanted to ask if its possible to create two simple bars using MScharts to look like this:

Screen shot http://img16.imageshack.us/img16/4413/desktoptf.png

I want an exact replica of the above image, so no diagram or grids, just two simple bars like these.

Another thing is that the max values are indicated by the small red stripes at each end of the bars and should stay there until the max value is surpassed.

The idea is that I'm reading live data (4 values) that are supposed to dynamically apply to the bars.

So does anybody know how to go about this? I am currently using the MScharts plugin (or would it maybe be better to use the paint function of C# instead of MScharts?).

Thanks in advance.

EDIT:

Ok, heres what I came up with:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool k = false;
        Random random = new Random();
        int max = 0;

        protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            int i = 30;
            Graphics gfx = paintEvnt.Graphics;
            Pen myPen = new Pen(Color.Black);

                for (i = 40; i < 640; i = i + 100)
                {
                    gfx.DrawLine(myPen, i, 25, i, 35); 
                }

                for (i = 40; i < 640; i = i + 100)
                {
                    gfx.DrawLine(myPen, i, 55, i, 65);
                }

                Color brushColor = Color.FromArgb(0, 0, 255);
                SolidBrush myBrush = new SolidBrush(brushColor);

                    int randomnumber = random.Next(0, 601);
                    gfx.FillRectangle(myBrush, 33, 33, randomnumber, 25);

                    if (randomnumber + 33 > max)
                    {
                        max = randomnumber + 33;
                        gfx.DrawLine(new Pen(Color.Red, 3), max, 30, max, 60);
                    }
                    else
                    {
                        gfx.DrawLine(new Pen(Color.Red, 3), max, 30, max, 60);
                    }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Invalidate();
        }
    }
}

And this is what it looks like: http://img411.imageshack.us/img411/5646/graphmj.jpg
Each time I press the button, new random data is generated and overrides the old graph. However, theres still a problem.
The red indicator is supposed to increase only when the max value has been exceeded and thats what I tried to implement with the if-query in the OnPaint method, yet it will still make up random values sometimes and go totally off, exceeding the old value eventhough the new random value is even lower.. that doesnt make sense.

Heres the project incase anyone wants to try out and help me:
http://up.k10x.net/ambglolrngulg/LevelMeter.zip

I'm really clueless because the code looks correct to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别再吹冷风 2024-12-22 14:36:26

礼貌:http://www.dotnetlines.com/Blogs/tabid/85/EntryId/44/Create-a-simple-Column-Chart-Bar-Chart-in-ASP-NET-using-C。 aspx

在 web.config 中添加如下 httphandler

<httpHandlers>

  <add path="ChartImg.axd"

       verb="GET,HEAD,POST"

       type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler,

       System.Web.DataVisualization,

       Version=4.0.0.0, Culture=neutral,

       PublicKeyToken=31bf3856ad364e35"

       validate="false"/>

</httpHandlers>

页面设计

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

CodeFile="Default.aspx.cs" Inherits="_Default" %>

 <%@ Register TagPrefix="asp" Namespace="System.Web.UI.DataVisualization.Charting"

Assembly="System.Web.DataVisualization,

        Version=4.0.0.0,

        Culture=neutral,

        PublicKeyToken=31bf3856ad364e35" %>

 //Place the chart control on the page as follows.

    <asp:Chart ID="Chart1" runat="server" Height="300px" Width="600px">

        <Titles>

            <asp:Title ShadowOffset="3" Name="Student Marks" />

        </Titles>

        <Legends>

            <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Legend"

                LegendStyle="Row" />

        </Legends>

        <Series>

            <asp:Series Name="Legend" />

        </Series>

        <ChartAreas>

            <asp:ChartArea Name="studentChartArea" BorderWidth="0" />

        </ChartAreas>

    </asp:Chart>

在 CodeBehind 中编写如下代码,用于填充 Chart 控件中的图表

protected void Page_Load(object sender, EventArgs e)

{

    string[] xAxis = { "Student1", "Student2", "Student3", "Student4", "Student5", "Student6" };

    double[] yAxis = { 39, 67, 96, 86, 47, 98 };

    Chart1.Series["Legend"].Points.DataBindXY(xAxis, yAxis);



    Chart1.Series["Legend"].Points[0].Color = Color.Black;

    Chart1.Series["Legend"].Points[1].Color = Color.Bisque;

    Chart1.Series["Legend"].Points[2].Color = Color.Blue;

    Chart1.Series["Legend"].Points[3].Color = Color.BlueViolet;

    Chart1.Series["Legend"].Points[4].Color = Color.Brown;

    Chart1.Series["Legend"].Points[5].Color = Color.CornflowerBlue;



    Chart1.Series["Legend"].ChartType = SeriesChartType.Column;     



    Chart1.ChartAreas["studentChartArea"].Area3DStyle.Enable3D = true;       

}

Courtesy : http://www.dotnetlines.com/Blogs/tabid/85/EntryId/44/Create-a-simple-Column-Chart-Bar-Chart-in-ASP-NET-using-C.aspx

Add the following httphandler in web.config

<httpHandlers>

  <add path="ChartImg.axd"

       verb="GET,HEAD,POST"

       type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler,

       System.Web.DataVisualization,

       Version=4.0.0.0, Culture=neutral,

       PublicKeyToken=31bf3856ad364e35"

       validate="false"/>

</httpHandlers>

Page design

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

CodeFile="Default.aspx.cs" Inherits="_Default" %>

 <%@ Register TagPrefix="asp" Namespace="System.Web.UI.DataVisualization.Charting"

Assembly="System.Web.DataVisualization,

        Version=4.0.0.0,

        Culture=neutral,

        PublicKeyToken=31bf3856ad364e35" %>

 //Place the chart control on the page as follows.

    <asp:Chart ID="Chart1" runat="server" Height="300px" Width="600px">

        <Titles>

            <asp:Title ShadowOffset="3" Name="Student Marks" />

        </Titles>

        <Legends>

            <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Legend"

                LegendStyle="Row" />

        </Legends>

        <Series>

            <asp:Series Name="Legend" />

        </Series>

        <ChartAreas>

            <asp:ChartArea Name="studentChartArea" BorderWidth="0" />

        </ChartAreas>

    </asp:Chart>

Write the following code in CodeBehind for filling the chart in the Chart control

protected void Page_Load(object sender, EventArgs e)

{

    string[] xAxis = { "Student1", "Student2", "Student3", "Student4", "Student5", "Student6" };

    double[] yAxis = { 39, 67, 96, 86, 47, 98 };

    Chart1.Series["Legend"].Points.DataBindXY(xAxis, yAxis);



    Chart1.Series["Legend"].Points[0].Color = Color.Black;

    Chart1.Series["Legend"].Points[1].Color = Color.Bisque;

    Chart1.Series["Legend"].Points[2].Color = Color.Blue;

    Chart1.Series["Legend"].Points[3].Color = Color.BlueViolet;

    Chart1.Series["Legend"].Points[4].Color = Color.Brown;

    Chart1.Series["Legend"].Points[5].Color = Color.CornflowerBlue;



    Chart1.Series["Legend"].ChartType = SeriesChartType.Column;     



    Chart1.ChartAreas["studentChartArea"].Area3DStyle.Enable3D = true;       

}
仙女山的月亮 2024-12-22 14:36:25

我不会为此使用 mschart。我会编写一个控件来执行条形和最大指示器,然后在另一个用户控件上使用它来获取标签和内容。
最后我发现 .net 中没有形状控制,因此这可能会给您一些线索。
开源 .net 形状控件

I wouldn't use mschart for that. I'd write a control to do the bar and max indicator and then use that on another usercontrol to get the labels and stuff.
Last I looked there's no shape control in .net, so this might give you a few clues.
open source .net shape control

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文