C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果
前言
上一篇文章C# Chart控件標(biāo)記問(wèn)題詳解,我們對(duì)C#Chart控件標(biāo)記問(wèn)題做了一系列的處理,今天是對(duì)上一篇文章的一個(gè)擴(kuò)展,使用鼠標(biāo)點(diǎn)擊事件對(duì)Chart上面的折線點(diǎn)進(jìn)行數(shù)據(jù)展示,是另外的一種展示方式,不明白的同學(xué)可以去看看我上一篇文章,這篇文章使用的方式就是點(diǎn)擊一下就彈出一個(gè)小的提示框,可以在提示框中寫我們自己想要寫的數(shù)據(jù),創(chuàng)作不易,大家點(diǎn)贊關(guān)注評(píng)論收藏,你的點(diǎn)贊是我創(chuàng)作的動(dòng)力,謝謝啦!??!
效果展示
使用tooltip的方式使用鼠標(biāo)點(diǎn)擊Chart中的折線,實(shí)現(xiàn)Chart的數(shù)據(jù)提示效果,需要使用鼠標(biāo)的點(diǎn)擊事件獲取點(diǎn)擊位置的x,y的坐標(biāo),并對(duì)這個(gè)坐標(biāo)在Chart折線圖中找到對(duì)應(yīng)的點(diǎn)的值,找到對(duì)應(yīng)點(diǎn)之后使用chart的point屬性對(duì)這個(gè)點(diǎn)的值的獲取,從而展示,后面有詳細(xì)的操作步驟,以及代碼邏輯,可以直接跳的最后面的解決方案去看。

解決方案
圖一圖二是對(duì)界面的一些的一些的設(shè)置


主要的代碼邏輯

整個(gè)項(xiàng)目的界面代碼,主要的這個(gè)功能主要的代碼在鼠標(biāo)的那個(gè)觸發(fā)函數(shù)的代碼,直接復(fù)制那個(gè)函數(shù)的代碼,粘貼就好了,不是很難。
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 System.Windows.Forms.DataVisualization.Charting;
namespace TestIC00
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
public int index = 0;
public int x = 0;
public bool flag = false;
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled;
}
private void timer1_Tick(object sender, EventArgs e)
{
Random random = new Random();
this.chart1.ChartAreas[0].AxisX.MajorGrid.Interval = 1;//網(wǎng)格間隔
this.chart1.ChartAreas[0].AxisX.MinorGrid.Interval = 1;
this.chart1.ChartAreas[0].AxisY.MajorGrid.Interval = 1;//網(wǎng)格間隔
this.chart1.ChartAreas[0].AxisY.MinorGrid.Interval = 1;
this.chart1.ChartAreas[0].AxisX.LabelStyle.Interval = 1;//設(shè)置X軸的值的間隔大小
this.chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.Gray;//設(shè)置X軸網(wǎng)格線顏色
this.chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Gray;//設(shè)置Y軸網(wǎng)格線顏色
chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;//啟動(dòng)滾動(dòng)條
this.chart1.ChartAreas[0].AxisY.LabelStyle.Enabled = false;//使Y軸的刻度隱藏
chart1.ChartAreas[0].AxisX.ScaleView.Scroll(System.Windows.Forms.DataVisualization.Charting.ScrollType.Last);//啟用視圖實(shí)現(xiàn)數(shù)據(jù)滾動(dòng)
int value = random.Next(0, 20);//產(chǎn)生隨機(jī)數(shù)進(jìn)行賦值
chart1.Series[0].Points.AddXY(x,value);//對(duì)折線圖添加數(shù)據(jù)
x++;
if (flag)//判斷標(biāo)記,如果是true表示只標(biāo)記最新,需要去掉前面的一個(gè)值
{
this.chart1.Series[0].Points[index].MarkerStyle = MarkerStyle.Circle;//設(shè)置標(biāo)記的形狀為圓形
this.chart1.Series[0].Points[index].MarkerColor = Color.Red;//形狀顏色設(shè)置
this.chart1.Series[0].Points[index].MarkerBorderWidth = 3;//形狀大小設(shè)置
this.chart1.Series[0].Points[index].MarkerSize = 10;//設(shè)置我們展示標(biāo)記的大小
this.chart1.Series[0].Points[index].Label = "功能:" + this.chart1.Series[0].Name + "\r\n" + "值:" + value.ToString();//對(duì)標(biāo)記展示的值
this.chart1.Series[0].Points[index].IsValueShownAsLabel = true;//展示標(biāo)記
this.chart1.Series[0].Points[index - 1].MarkerBorderWidth = 0;//改前一個(gè)標(biāo)記的大小
this.chart1.Series[0].Points[index - 1].MarkerSize = 0;//形狀大小
this.chart1.Series[0].Points[index - 1].Label = "";//展示數(shù)據(jù)
this.chart1.Series[0].Points[index - 1].IsValueShownAsLabel = false;//不展示
}
else//對(duì)數(shù)據(jù)一直標(biāo)記
{
this.chart1.Series[0].Points[index].MarkerStyle = MarkerStyle.Circle;
this.chart1.Series[0].Points[index].MarkerColor = Color.Red;
this.chart1.Series[0].Points[index].MarkerBorderWidth = 3;
this.chart1.Series[0].Points[index].MarkerSize = 10;
this.chart1.Series[0].Points[index].Label = "功能:" + this.chart1.Series[0].Name + "\r\n" + "值:" + value.ToString();
this.chart1.Series[0].Points[index].IsValueShownAsLabel = true;
}
//也可以加一種狀態(tài)是什么也不標(biāo)記,你們自己對(duì)那個(gè)狀態(tài)值的處理就可以啦
index++;
}
private void button2_Click(object sender, EventArgs e)
{
flag = !flag;
}
private void chart1_MouseClick(object sender, MouseEventArgs e)
{
HitTestResult mytestresult = new HitTestResult();//它表示命中測(cè)試的返回值
mytestresult = chart1.HitTest(e.X, e.Y);//獲取我們Chart控件的折線圖在這個(gè)坐標(biāo)的值
if (e.Button == MouseButtons.Left)//判斷是否是鼠標(biāo)左鍵點(diǎn)擊
{
if (mytestresult.ChartElementType == ChartElementType.DataPoint)//判斷我們點(diǎn)擊這個(gè)返回集是否是chart的數(shù)據(jù)點(diǎn)的類型
{
toolTip1.AutoPopDelay = 5000;//表示tooltip在這個(gè)控件中保留展示的時(shí)間
toolTip1.InitialDelay = 1000;//表示鼠標(biāo)指針必須在這里靜止的時(shí)間
toolTip1.ReshowDelay = 500;//可以縮短或延長(zhǎng)在顯示上一個(gè)工具提示窗口后顯示工具提示窗口之前等待的時(shí)間tooltip
toolTip1.ShowAlways = true;//獲取或設(shè)置一個(gè)值,該值指示是否顯示工具提示窗口,甚至是在其父控件不活動(dòng)的時(shí)候。
try
{
toolTip1.SetToolTip(chart1, "名稱:" + mytestresult.Series.Name + "\r\n" +"值:"+chart1.Series[0].Points[mytestresult.PointIndex]);//設(shè)置ToolTip展示的值的內(nèi)容,mytestresult.Series.Name表示折線的名字,chart1.Series[0].Points[mytestresult.PointIndex]表示chart圖表中的第0(從0開(kāi)始也就是第一條)條折線的數(shù)據(jù)點(diǎn)的第多少個(gè),
//也可以設(shè)置chart1.Series[0].Points[mytestresult.PointIndex].XValue;這個(gè)代表你點(diǎn)擊這個(gè)點(diǎn)的x軸的值,chart1.Series[0].Points[mytestresult.PointIndex].YValues[0];這個(gè)代表你點(diǎn)擊的點(diǎn)Y軸的值
}
catch (Exception)
{
}
}
}
}
}
}總結(jié)
這篇文章是對(duì)上一篇文章的擴(kuò)展,使用的是ToolTip技術(shù)和鼠標(biāo)點(diǎn)擊的事件,再加上測(cè)試返回集,雖然很簡(jiǎn)單,但是也是可以學(xué)到東西的,C#的技術(shù)就是先簡(jiǎn)單再難嘛,積少成多之后才會(huì)成長(zhǎng)才會(huì)進(jìn)步,我們要不斷的學(xué)習(xí)不斷的探索,才能有學(xué)習(xí)的動(dòng)力,才會(huì)有學(xué)習(xí)的欲望
到此這篇關(guān)于C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果的文章就介紹到這了,更多相關(guān)C# Chart控件 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#創(chuàng)建WebService接口并連接的全過(guò)程
工作時(shí)遇到需要請(qǐng)求客戶的接口返回?cái)?shù)據(jù),要求使用WebService,借此機(jī)會(huì)記錄一下,下面這篇文章主要給大家介紹了關(guān)于C#創(chuàng)建WebService接口并連接的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
UnityShader3實(shí)現(xiàn)轉(zhuǎn)圈與冷卻效果
這篇文章主要為大家詳細(xì)介紹了UnityShader3實(shí)現(xiàn)轉(zhuǎn)圈與冷卻效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
C#?SetWindowPos函數(shù)實(shí)例詳解
在C#中,SetWindowPos函數(shù)用于設(shè)置窗口的位置和大小,這篇文章主要介紹了C#?SetWindowPos函數(shù)實(shí)例詳解,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
舊項(xiàng)目升級(jí)新版Unity2021導(dǎo)致Visual?Studio無(wú)法使用的問(wèn)題
在項(xiàng)目開(kāi)發(fā)過(guò)程中,不可避免的會(huì)升級(jí)開(kāi)發(fā)工具。這次我在舊項(xiàng)目版本升級(jí)到新版Unity2021.2.x時(shí),出現(xiàn)Visual?Studio無(wú)法定位等問(wèn)題,這里我給大家分享下解決方法,舊項(xiàng)目升級(jí)新版Unity2021導(dǎo)致Visual?Studio無(wú)法使用的問(wèn)題,需要的朋友可以參考下2021-12-12

