天天看点

再探MSCHART

 你定义数组的时候不要制定类型,定义成Variant类型,dim mydata(0 to n,0 to 1) As variant,这样没赋值的部分就是NULL,就没有线段,否则当定义成Double或其他类型时,如果没有赋值的部分都是0,这样他就是原点,所以最终曲线都是回到原点,就形成了折线      
.AllowDithering = True
  .AllowDynamicRotation = True
  .AllowSelections = True  可以選擇點
  .AllowSeriesSelection = True  可以選擇整條線
  .AutoIncrement = True
  .ShowLegend = True   顯示圖例名稱      
 .Legend.Location.LocationType = VtChLocationTypeBottomRight      

ChartAreas:增加多個繪圖區域,每個繪圖區域包含獨立的圖表組、資料源,用於多個圖表類型在一個繪圖區不相容時。

AlignmentOrientation:圖表區對齊方向,定義兩個繪圖區域間的對齊方式。

AlignmentStyle:圖表區對齊類型,定義圖表間用以對其的元素。

AlignWithChartArea:參照對齊的繪圖區名稱。

InnerPlotPosition:圖表在繪圖區內的位置屬性。

Auto:是否自動對齊。

Height:圖表在繪圖區內的高度(百分比,取值在0-100)

Width:圖表在繪圖區內的寬度(百分比,取值在0-100)

X,Y:圖表在繪圖區內左上角座標

Position:繪圖區位置屬性,同InnerPlotPosition。

Name:繪圖區名稱。

Axis:坐標軸集合

Title:坐標軸標題

TitleAlignment:坐標軸標題對齊方式

Interval:軸刻度間隔大小

IntervalOffset:軸刻度偏移量大小

MinorGrid:次要輔助線

MinorTickMark:次要刻度線

MajorGrid:主要輔助線

MajorTickMark:主要刻度線

DataSourceID:MSChart的數據源。

Legends:圖例說明。

IsVisibleInLegend =false;                         //是否顯示圖例說明  

IsValueShownAsLabel = true;

IsValueShownAsLabel:是否顯示資料點標籤,如果為true,在圖表中顯示每一個資料值

Label:資料點標籤文本

  Chart1.Series[1].IsValueShownAsLabel = true;             //是否顯示資料      

       Chart1.Series[1].IsVisibleInLegend = false;              //是否顯示資料說明  

       Chart1.Series[1].MarkerStyle = MarkerStyle.Circle;        //線條上的資料點標誌類型  

       Chart1.Series[1].MarkerSize = 8;                          //標誌大小

Palette:圖表外觀定義。

Series:最重要的屬性,圖表集合,就是最終看到的圓形圖、柱狀圖、線圖、點圖等構成的集合;可以將多種相互相容的類型放在一個繪圖區域內,形成複合圖。

LabelFormat:資料點標籤文本格式

LabelAngle:標籤字體角度

Name:圖表名稱

Points:資料點集合

XValueType:橫坐標軸類型

YValueType:縱坐標軸類型

XValueMember:橫坐標綁定的資料源(如果資料源為Table,則填寫橫坐標要顯示的欄位名稱)

YValueMembers:縱坐標綁定的資料源(如果資料源為Table,則填寫縱坐標要顯示的欄位名稱,縱坐標可以有兩個)

ChartArea:圖表所屬的繪圖區域名稱

ChartType:圖表類型(柱形、餅形、線形、點形等)

Legend:圖表使用的圖例名稱

Titles:標題集合。

width:MSChart的寬度。

height:MSChart的高度。

MSChart1.Row= 1  

MSChart1.RowLabel ="First row" 

MSChart1.Column= 3  

MSChart1.ColumnLabel ="Blue"

根據你的要求,我簡單做了一個日期和收入的對應圖,代碼及調試結果如下所示      
Private Sub Command1_Click()      
          Dim i   As Integer      
          Dim conn  As New ADODB.Connection      
          Dim rs     As New ADODB.Recordset      
          Set conn = New ADODB.Connection      
          Set rs = New ADODB.Recordset      
                             conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\lwl.mdb"      
          conn.Open      
          rs.Open "select  *  from   lwl", conn, adOpenKeyset, adLockOptimistic      
        '  Set MSChart1.DataSource = rs      
  With MSChart1      
'// 以線條方式顯示      
      .chartType = 3      
'// 圖表標題      
.TitleText = "SMT監控"      
//页底说明
MSChart.Footnote.Text = "说明:这是测试统计图表";      
 //设置Plot的Shadow
MSChart.Plot.Backdrop.Shadow.Style = VtShadowStyle.VtShadowStyleDrop;
 MSChart.Plot.Backdrop.Shadow.Offset.Set(60, 60);//'设置Shadow的大小设置Plot的边框
MSChart.Plot.Backdrop.Frame.Style = VtFrameStyle.VtFrameStyleSingleLine;
 设置Plot的背景色
 MSChart.Plot.Backdrop.Fill.Style = VtFillStyle.VtFillStyleBrush;
 MSChart.Plot.Backdrop.Fill.Brush.FillColor.Set(255, 255, 255);      
//取消竖的导航线
            _MSChart.Plot.get_Axis(VtChAxisId.VtChAxisIdX, 0).AxisGrid.MajorPen.Style = VtPenStyle.VtPenStyleNull;
            _MSChart.Plot.get_Axis(VtChAxisId.VtChAxisIdX, 0).AxisScale.Hide = true;      
'    '繪圖筆設置      
    .Plot.SeriesCollection(1).Pen.Width = 20      
    .Plot.SeriesCollection(1).Pen.Style = VtPenStyleSolid      
'    '自動可視範圍調整(自動縮放)      
    .Plot.Axis(VtChAxisIdX).ValueScale.Auto = False      
    '.Plot.Axis(VtChAxisIdY).ValueScale.Auto = False      
    '//標題字體      
    .Plot.Axis(VtChAxisIdX).AxisTitle.VtFont.Size = 12      
    .Plot.Axis(VtChAxisIdY).AxisTitle.VtFont.Size = 12      
    '設置XY標題      
    .Plot.Axis(VtChAxisIdX).AxisTitle = "時間(s)"      
    .Plot.Axis(VtChAxisIdY).AxisTitle = "速度cm/s"      
    '// 最大值      
    .Plot.Axis(VtChAxisIdX).ValueScale.Maximum = 600      
    .Plot.Axis(VtChAxisIdY).ValueScale.Maximum = 100      
    '// 最小值      
    .Plot.Axis(VtChAxisIdY).ValueScale.Minimum = 0      
    .Plot.Axis(VtChAxisIdX).ValueScale.Minimum = 0      
    '//主輔助線      
    .Plot.Axis(VtChAxisIdX).ValueScale.MajorDivision = 30 'X禸璶      
    .Plot.Axis(VtChAxisIdY).ValueScale.MajorDivision = 20 'Y禸璶      
‘     不顯示則設為majorpen.style=vtpenstylenull      
    ‘//輔助輔助線      
    .Plot.Axis(VtChAxisIdX).ValueScale.MinorDivision = 0 'X禸Ω璶      
    .Plot.Axis(VtChAxisIdY).ValueScale.MinorDivision = 0 'Y禸Ω璶      
‘//線型設置      
    .Plot.Axis(VtChAxisIdX).AxisGrid.MajorPen.Style = VtPenStyleDotted      
    .Plot.Axis(VtChAxisIdY).AxisGrid.MajorPen.Style = VtPenStyleDotted      
    .Plot.AutoLayout = False  ‘座標佈局人工還是自動      
    .Plot.UniformAxis = False   '座標單位刻度是否一致      
      '// 增加測試資料      
      .ColumnCount = 1      
      '// 將圖表作為圖例的背景。      
      .ShowLegend = False      
      '// 標記每個點的值      
      For i = 1 To .Plot.SeriesCollection.Count      
         .Plot.SeriesCollection(i).DataPoints(-1).DataPointLabel.LocationType = VtChLabelLocationTypeAbovePoint      
      Next      
               .Row = i       
         .RowLabel = CStr(rs("日期"))      
          .Data = rs("收入")      
    With Chart1.Plot.Axis(VtChAxisIdX)   'X軸設置      
        '.ValueScale.MajorDivision = 24      
        '.ValueScale.MinorDivision = 24      
        .CategoryScale.Auto = False         '設置為人工縮放。      
        .CategoryScale.DivisionsPerLabel = 12 '   '每12個單位顯示標籤。      
        .CategoryScale.DivisionsPerTick = 12  '   '每12個單位顯示刻度。      
        .CategoryScale.LabelTick = True   '在刻度標記頂端顯示標籤。      
    End With      
修改X座標值      
XMSChart1.Row = 1: MSChart1.RowLabel = "1"      

MSChart1.Plot.DataSeriesInRow = True

For xCount = 1 To MSChart1.RowCount

   WithMSChart1.Plot.SeriesCollection(xCount).DataPoints(-1).DataPointLabel

      .LocationType = VtChLabelLocationTypeAbovePoint

      .Component = VtChLabelComponentPercent

      .ValueFormat = "#####0.00" '設定資料點格式

      .PercentFormat = "0.0%"

   End With

Next xCount

With MSChart1.Plot.Axis(VtChAxisIdX).ValueScale
    .Auto = False
    .Maximum = 50
    .Minimum = 0
    .MajorDivision = 5
    .MinorDivision = 2
End With
With MSChart1.Plot.Axis(VtChAxisIdY).ValueScale
    .Auto = False
    .Maximum = 100
    .Minimum = 0
    .MajorDivision = 10
    .MinorDivision = 2
End With      
'改变图例的位置:      
MSChart1.Legend.Location.LocationType = VtChLocationTypeTop'图例在图表上方(具体参数vb会自动列出,看意思就知道了)      
'改变plot的大小:      
'通过Plot.LocationRect.Min.x和Plot.LocationRect.Min.y确定绘图区的左下方坐标;通过Plot.LocationRect.Max.x和Plot.LocationRect.Max.y确定绘图区的右上方坐标      
With MSChart1      
    .Plot.AutoLayout = False      
    .Plot.LocationRect.Min.Set 0, 0      
    .Plot.LocationRect.Max.Set MSChart1.Width, MSChart1.Height      
End With      

Plot 对象

          表示图表显示的区域。

语法

Plot

说明

Plot 对象允许您编程以下对象:

  1. Axis 对象—代表图表的 x 、 y 和 z 轴。z 轴仅在立体图表中是可见的。
  2. BackDrop 对象—轴后面的区域。
  1. Light 对象—图的环境和边缘光。
  1. LocationRect 对象—图的位置。
  1. SeriesCollection 对象—序列的集合。
  1. View3D 对象—立体图像的正视图和旋转图。
  2. Wall 对象—图后面的区域

继续阅读