免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 回復 發帖

CHART 相關

使用Chart中ChartArea的AxisX.ValueToPixelPosition()就可以將X軸的座標,轉換成以Chart左上角為(0,0)的座標..

而PixelPositionToValue可以將在CHart中的座標轉換為X軸的座標...

這樣一來就可以知道當滑鼠點到Chart中.他的座標會對應到X軸的那一個值...

一開始他的AxisX.ValueToPixelPosition()要輸入的型態是Double而不是Point,所以我搞不清楚要輸入什麼..

後來才試出來原來是要輸入Point中的X值或是Y值去轉換....

不過,ValueToPixelPosition必須要在繪完圖後才能轉換,不然會發生錯誤,因為圖還沒畫好,電腦也不知道要怎麼轉.....

------------------------------------------------------------------------
for (k = 1; k <= 1000; k++)
{

Chart1.Series["Series1"].Points.SuspendUpdates();
Chart1.Series["Series1"].Points.AddXY(k, FLine1[k - 1]);
Chart1.Series["Series2"].Points.AddXY(k, FLine2[k - 1]);
Chart1.Series["Series3"].Points.AddXY(k, FLine3[k - 1]);
Chart1.Series["Series1"].Points.ResumeUpdates();
  }
------------------------------------------------------------------
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;
using System.Windows.Forms.DataVisualization.Charting;

namespace chart_test
{
public partial class Form1 : Form
{
DateTime dt;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
ChartArea area1 = chart1.ChartAreas[0];

timer1.Enabled = true;
SeriesTimer.Enabled = true;

area1.AxisX.LabelStyle.Format = "HH:mm:ss.fff";
area1.AxisX.ScaleView.Size = 5;
area1.AxisX.ScrollBar.IsPositionedInside = true;
area1.AxisX.ScrollBar.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
dt = DateTime.Now;
label2.Text = dt.ToString("HH:mm:ss") + "." + dt.ToString("FFF").PadRight(3, '0');
}

private void SeriesTimer_Tick(object sender, EventArgs e)
{
DateTime Gtime1 = DateTime.Now;
Series series0 = chart1.Series[0];
ChartArea area1 = chart1.ChartAreas[0];

series0.Points.AddXY(Gtime1.ToString("HH:mm:ss") + "." + Gtime1.ToString("FFF").PadRight(3, '0'), 0);
area1.AxisX.ScaleView.Position = series0.Points.Count - 5;
}

private void button1_Click(object sender, EventArgs e)
{
Series series1 = chart1.Series[1];
ChartArea area1 = chart1.ChartAreas[0];
label3.Text = label2.Text;
series1.Points.AddXY(label2.Text, 1);
}

private void button2_Click(object sender, EventArgs e)
{
Series series1 = chart1.Series[1];
ChartArea area1 = chart1.ChartAreas[0];

label4.Text = label2.Text;
series1.Points.AddXY(label2.Text, 2);
}
private void button3_Click(object sender, EventArgs e)
{
Series series1 = chart1.Series[1];
ChartArea area1 = chart1.ChartAreas[0];

label5.Text = label2.Text;
series1.Points.AddXY(label2.Text, 3);
}

private void button4_Click(object sender, EventArgs e)
{
Series series1 = chart1.Series[1];
ChartArea area1 = chart1.ChartAreas[0];

label6.Text = label2.Text;
series1.Points.AddXY(label2.Text, 4);


------------------------------------------------------------

// Set scrollbar size
chart1.ChartAreas["Default"].AxisX.ScrollBar.Size = 10;

// Show small scroll buttons only
chart1.ChartAreas["Default"].AxisX.ScrollBar.Buttons = ScrollBarButtonStyle.SmallScroll;

// Scrollbars position
chart1.ChartAreas["Default"].AxisX.ScrollBar.IsPositionedInside = true;

// Change scrollbar colors
chart1.ChartAreas["Default"].AxisX.ScrollBar.BackColor = Color.LightGray;
chart1.ChartAreas["Default"].AxisX.ScrollBar.ButtonColor = Color.Gray;
chart1.ChartAreas["Default"].AxisX.ScrollBar.LineColor = Color.Black;

...




------------------------------------------------------------------------
Custom Labels

Imports System.Windows.Forms.DataVisualization.Charting
...

Dim element As Integer

' Set X axis custom labels
Dim element As Integer = Chart1.ChartAreas("Default").AxisY.CustomLabels.Add(0, 30, "Low")
element = Chart1.ChartAreas("Default").AxisY.CustomLabels.Add(30, 70, "Medium")
element = Chart1.ChartAreas("Default").AxisY.CustomLabels.Add(70, 100, "High")

' Set Y axis custom labels
element = Chart1.ChartAreas("Default").AxisX.CustomLabels.Add(0.5, 1.5, "Jan")
Chart1.ChartAreas("Default").AxisX.CustomLabels(element).GridTicks = GridTickTypes.All

element = Chart1.ChartAreas("Default").AxisX.CustomLabels.Add(1.5, 2.5, "Feb")
Chart1.ChartAreas("Default").AxisX.CustomLabels(element).GridTicks = GridTickTypes.TickMark

element = Chart1.ChartAreas("Default").AxisX.CustomLabels.Add(2.5, 3.5, "Mar")
Chart1.ChartAreas("Default").AxisX.CustomLabels(element).GridTicks = GridTickTypes.All

element = Chart1.ChartAreas("Default").AxisX.CustomLabels.Add(3.5, 4.5, "Apr")
Chart1.ChartAreas("Default").AxisX.CustomLabels(element).GridTicks = GridTickTypes.TickMark

element = Chart1.ChartAreas("Default").AxisX.CustomLabels.Add(4.5, 5.5, "May")
Chart1.ChartAreas("Default").AxisX.CustomLabels(element).GridTicks = GridTickTypes.All

' set second row of labels
Chart1.ChartAreas("Default").AxisX.CustomLabels.Add(0.5, 3.5, "Q1", 1, LabelMarkStyle.LineSideMark)
Chart1.ChartAreas("Default").AxisX.CustomLabels.Add(3.5, 5.5, "Q2", 1, LabelMarkStyle.LineSideMark)

' One more row of labels
Chart1.ChartAreas("Default").AxisX.CustomLabels.Add(0.5, 5.5, "Year 2006", 2, LabelMarkStyle.LineSideMark)
...

-------------------------------------------------------
可以手动调整数据为时间格式的数据
  series.XValueType = ChartValueType.Time;
series.ToolTip = "时间:#VALX 值#VALY";
series.Points.AddXY(DateTime.Parse(item.time.ToString("H:mm:ss")).ToOADate(),
ConvertToDoubleFromString(obj.传输数据));
chart.Series.Add(series);

-------------------------------------------------
While Chart1.Series.Count > 3
      Chart1.Series.RemoveAt(3)
   End While
   While Chart1.ChartAreas.Count > 1
      Chart1.ChartAreas.RemoveAt(1)
   End While

------------------------------------------


Imports System.Windows.Forms.DataVisualization.Charting
...

' Set chart area to be visible on the chart
Chart1.ChartAreas("Default").Visible = true

' Set the chart area alignmnet. This will cause the axes to align vertically.
Chart1.ChartAreas("Chart Area 2").AlignmentOrientation = AreaAlignmentOrientations.Vertical

' Disable X Axis Labels for the first chart area.
Chart1.ChartAreas("Default").AxisX.LabelStyle.Enabled = False

' Disable End labels for Y axes.
Chart1.ChartAreas("Default").AxisY.LabelStyle.IsEndLabelVisible = False
Chart1.ChartAreas("Chart Area 2").AxisY.LabelStyle.IsEndLabelVisible = False

' Set the chart area position for the first chart area.
Chart1.ChartAreas("Default").Position.X = 5
Chart1.ChartAreas("Default").Position.Y = 10
Chart1.ChartAreas("Default").Position.Width = 90
Chart1.ChartAreas("Default").Position.Height = 40

' Set the chart area position for the second chart area.
Chart1.ChartAreas("Chart Area 2").Position.X = 5
Chart1.ChartAreas("Chart Area 2").Position.Y = 50
Chart1.ChartAreas("Chart Area 2").Position.Width = 90
Chart1.ChartAreas("Chart Area 2").Position.Height = 40

------------------------------------------------------
Chart1.Series("Series1").ChartArea = "Default"
   
' Attach the second series to a chart area.
Chart1.Series("Series2").ChartArea = "Chart Area 2"
   
' Remove series from chart areas.
Chart1.Series("Series3").ChartArea = ""

Imports System.Windows.Forms.DataVisualization.Charting
...

' Set legend style
Chart1.Legends("Default").LegendStyle = LegendStyle.Column

' Set legend docking
Chart1.Legends("Default").Docking = Docking.Right

' Set legend alignment
Chart1.Legends("Default").Alignment = StringAlignment.Center

If InsideChartArea.Checked Then
' Display legend inside the "Default" chart area
    Chart1.Legends("Default").InsideChartArea = "Default"
Else
' Do not display legend inside the "Default" chart area
    Chart1.Legends("Default").InsideChartArea = ""
End If

' Disable legend
If LegendDisabled.Checked Then
    Chart1.Legends("Default").Enabled = False
End If

Chart1.Legends("Default").IsEquallySpacedItems = False   

-----------------------------------------------





Imports System.Windows.Forms.DataVisualization.Charting
...

Private Sub Save_Click(sender As Object, e As System.EventArgs)
   
   ' Create a new save file dialog
   Dim saveFileDialog1 As New SaveFileDialog()
   
   ' Sets the current file name filter string, which determines
   ' the choices that appear in the "Save as file type" or
   ' "Files of type" box in the dialog box.
   saveFileDialog1.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|SVG (*.svg)|*.svg|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif"
   saveFileDialog1.FilterIndex = 2
   saveFileDialog1.RestoreDirectory = True
   
   ' Set image file format
   If saveFileDialog1.ShowDialog() = DialogResult.OK Then
      Dim format As ChartImageFormat = ChartImageFormat.Bmp
      
      If saveFileDialog1.FileName.EndsWith("bmp") Then
         format = ChartImageFormat.Bmp
      Else
         If saveFileDialog1.FileName.EndsWith("jpg") Then
            format = ChartImageFormat.Jpeg
         Else
            If saveFileDialog1.FileName.EndsWith("emf") Then
               format = ChartImageFormat.Emf
            Else
               If saveFileDialog1.FileName.EndsWith("gif") Then
                  format = ChartImageFormat.Gif
               Else
                  If saveFileDialog1.FileName.EndsWith("png") Then
                     format = ChartImageFormat.Png
                  Else
                     If saveFileDialog1.FileName.EndsWith("tif") Then
                        format = ChartImageFormat.Tiff
                     Else
                        If saveFileDialog1.FileName.EndsWith("svg") Then
                           format = ChartImageFormat.Svg
                        End If
                     End If
                  End If ' Save image
               End If
            End If
         End If
      End If
      Chart1.SaveImage(saveFileDialog1.FileName, format)
   End If
   
End Sub 'Save_Click

Private Sub Copy_Click(sender As Object, e As System.EventArgs)
   
   ' Create a memory stream to save the chart image   
   Dim stream As New System.IO.MemoryStream()
   
   ' Save the chart image to the stream   
   Chart1.SaveImage(stream, System.Drawing.Imaging.ImageFormat.Bmp)
   
   ' Create a bitmap using the stream   
   Dim bmp As New Bitmap(stream)
   
   ' Save the bitmap to the clipboard   
   Clipboard.SetDataObject(bmp)
   
End Sub 'Copy_Click
返回列表