c# - 从 MySql 数据创建刻度盘和图表

标签 c# mysql graphics

我刚刚创建了一个简单的 Windows 窗体来显示 MySql 表中的一些数据。

我想知道的是如何使用一些刻度盘和数据历史图表以图形方式显示这些数据。

这就是我到目前为止所做的...当您单击按钮时,它开始读取并在 2 个文本框中显示数据。当您再次单击该按钮时,它会停止。

我弄清楚了如何添加折线图,仍然需要知道如何添加“刻度盘而不仅仅是文本框...

使用 aGauge 解决了这个问题...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        MySqlConnection mcon = new MySqlConnection("Server=www.inshome.net.au;Port=3306;Database=wordpress;Uid=root;password=********");
        MySqlCommand mcd;
        MySqlDataReader mdr;
        string s;

        public Form1()
        {
            InitializeComponent();
        }

        private void getTemp_Click(object sender, EventArgs e)
        {
            // Toggle the timer's enabled state
            timer1.Enabled = !timer1.Enabled;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                mcon.Open();
                s = "SELECT time, dht22temp, dht22humidity FROM shed ORDER BY time DESC LIMIT 1";
                mcd = new MySqlCommand(s, mcon);
                mdr = mcd.ExecuteReader();
                if (mdr.Read())
                {
                    shedTemp.Text = mdr.GetString("dht22temp");
                    shedHumidity.Text = mdr.GetString("dht22humidity");
                    aGauge1.Value = float.Parse(mdr.GetString("dht22temp"));
                    aGauge2.Value = float.Parse(mdr.GetString("dht22humidity"));
                    this.chart1.Series["Temp"].Points.AddXY(mdr.GetString("time"), mdr.GetString("dht22temp"));
                    this.chart2.Series["Humidity"].Points.AddXY(mdr.GetString("time"), mdr.GetString("dht22humidity"));
                }
                else
                {
                    MessageBox.Show("NO DATA");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                mdr.Close();
                mcon.Close();
            }
        }
    }
}

最佳答案

VS 工具箱中没有任何东西可以为您做到这一点。

因此,您要么必须寻找一个库,要么使用图形化程度较低的东西来解决问题,例如样式漂亮的文本显示或完全扭曲的饼图。或者你必须自己制作。

这是一个应该帮助您入门的示例:

enter image description here

class DialLabel : Label
{
    public DialLabel()
    {
        AutoSize = false;
        NeedleColor = Color.Red;
        NeedleWidth = 3f;
    }

    public Color NeedleColor { get; set; }
    public float NeedleWidth { get; set; }
    public string Format { get; set; }

    public decimal MaxValue { get; set; }
    public decimal Value { get; set; }
    float angle { get { return 180f / 100f * (float)(MaxValue - Value); } }

    protected override void OnPaint(PaintEventArgs e)
    {
        Point center = new Point(Width / 2, (Height - 25)); // 25 pixels for text
        Rectangle textbox = new Rectangle(1, Height - 25, Width, 25);
        TextRenderer.DrawText(e.Graphics, Value.ToString(Format), Font,
                              textbox, ForeColor, BackColor);
        float nx = Width / 2f + 
                  (Width / 2f - 2) * (float)Math.Cos(angle * Math.PI / 180f);
        float ny = (Height - 25) - 
                   (Width / 2f - 2) * (float)Math.Sin(angle * Math.PI / 180f);

        using (Pen pen = new Pen(NeedleColor, NeedleWidth))
            e.Graphics.DrawLine(pen, center.X, center.Y, nx, ny);
    } 
}

表盘标签公开了多个可用于基本样式设置的属性。将Text设置为String.Empty并选择一个漂亮的Image

如果将Format设置为“##0 km/h”并将ForeColor更改为白色,将BackColor更改为 为透明。

当您研究了代码后,您可以直接更改许多细节,所有其他细节..

要使用它,请将类添加到项目中,编译并将其从工具箱顶部添加到表单中。

要更改值,请使用以下内容:

private void trackBar1_Scroll(object sender, EventArgs e)
{
    dialLabel1.Value = trackBar1.Value;
    dialLabel1.Invalidate();
}

关于c# - 从 MySql 数据创建刻度盘和图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177384/

相关文章:

c# - 动态函数列表并动态调用它们

C# "Generator"方法

c# - SwaggerUI 不显示枚举摘要描述,C# .net 核心?

php - 从 MySQL 中连续选择随机用户

php - 从 MySQL 数据库中选择随机问题; "correct answer"搞砸了

c# - 在 C# 中的图片框上绘制箭头

c# - WPF 忽略 d :IsHidden ="True" in run mode (OK in design mode)

php - mysql校验数据sha512加密

java - 我们如何在 Swing 中实现类似 excel 的功能?

c# - 在 3D 地形上,给定一条 3D 线,找到线和地形之间的交点