OWC 的应用 - 柱状图(VB.NET)

  ' 创建ChartSpace对象来放置图表

        
Dim  myChart  As  ChartSpaceClass  =   New  ChartSpaceClass

        
' 在ChartSpace对象中添加图表,Add方法返回chart对象

        
Dim  Chart1  As  ChChart  =  myChart.Charts.Add( 0 )

        
' 指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到

        Chart1.Type 
=  OWC10.ChartChartTypeEnum.chChartTypeColumnClustered



        
' 指定图表是否需要图例

        Chart1.HasLegend 
=   True

        Chart1.Legend.Position 
=  ChartLegendPositionEnum.chLegendPositionTop



        
' 给定标题

        Chart1.HasTitle 
=   True

        Chart1.Title.Caption 
=   " 1-6月数据分布图 "



        
' 计算数据

        
' X轴

        
Dim  strCategory  As   String   =   " 1 "   +  ControlChars.Tab  +   " 2 "   +  ControlChars.Tab _

                                 
+   " 3 "   +  ControlChars.Tab  +   " 4 "   +  ControlChars.Tab  +   " 5 "   +  ControlChars.Tab _

                                 
+   " 6 "   +  ControlChars.Tab

        
' Y轴

        
Dim  strValue  As   String   =   " 9 "   +  ControlChars.Tab  +   " 8 "   +  ControlChars.Tab _

                                 
+   " 4 "   +  ControlChars.Tab  +   " 10 "   +  ControlChars.Tab  +   " 12 "   +  ControlChars.Tab _

                                 
+   " 6 "   +  ControlChars.Tab

        
' Z轴

        
Dim  strValue2  As   String   =   " 0.3 "   +  ControlChars.Tab  +   " 0.63 "   +  ControlChars.Tab _

                                  
+   " 0.48 "   +  ControlChars.Tab  +   " 0.86 "   +  ControlChars.Tab  +   " 0.95 "   +  ControlChars.Tab _

                                  
+   " 0.18 "   +  ControlChars.Tab





        
' 1. 添加一个series

        
Dim  Ser1  As  ChSeries  =  Chart1.SeriesCollection.Add( 0 )

        
' 给定series的名字

        Ser1.SetData(ChartDimensionsEnum.chDimSeriesNames, _

                                           ChartSpecialDataSourcesEnum.chDataLiteral, 
" Product " )

        
' 给定分类

        Ser1.SetData(ChartDimensionsEnum.chDimCategories, _

                                           ChartSpecialDataSourcesEnum.chDataLiteral, strCategory)

        
' 给定值

        Ser1.SetData(ChartDimensionsEnum.chDimValues, _

                                           ChartSpecialDataSourcesEnum.chDataLiteral, strValue)





        
' 2. 再添加一个series

        
Dim  Ser2  As  ChSeries  =  Chart1.SeriesCollection.Add( 1 )

        
' 给定series的名字

        Ser2.SetData(ChartDimensionsEnum.chDimSeriesNames, _

                                           ChartSpecialDataSourcesEnum.chDataLiteral, 
" Ratio " )

        
' 给定分类

        Ser2.SetData(ChartDimensionsEnum.chDimCategories, _

                                           ChartSpecialDataSourcesEnum.chDataLiteral, strCategory)

        
' 给定值

        Ser2.SetData(ChartDimensionsEnum.chDimValues, _

                                           ChartSpecialDataSourcesEnum.chDataLiteral, strValue2)

        Ser2.Ungroup(
True )

        Ser2.Type 
=  ChartChartTypeEnum.chChartTypeLineMarkers

        
' 添加Z轴

        
Dim  AxesZ  As  ChAxis  =  Chart1.Axes.Add(Ser2.Scalings(ChartDimensionsEnum.chDimValues))

        AxesZ.NumberFormat 
=   " 0.00% "

        AxesZ.Position 
=  ChartAxisPositionEnum.chAxisPositionRight





        
' 给定x,y轴的图示说明

        
' 默认只有X,Y 两个轴

        Chart1.Axes(
- 3 ).HasTitle  =   True

        Chart1.Axes(
- 3 ).Title.Caption  =   " Y 轴 : 数量 "

        Chart1.Axes(
- 2 ).HasTitle  =   True

        Chart1.Axes(
- 2 ).Title.Caption  =   " X 轴: 月份 "

        AxesZ.HasTitle 
=   True

        AxesZ.Title.Caption 
=   " Z 轴: Ratio "





        
' 输出成GIF文件.

        
Dim  strAbsolutePath  As   String   =   " C:Images "   &   Format ( Date .Now,  " yyyyMMddhhmmss " &   " .gif "

        myChart.ExportPicture(strAbsolutePath, 
" GIF " 600 350 )

        
Me .Image1.ImageUrl  =  strAbsolutePath
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如何安装mschart#Region "定义变量" Const XOffset As Integer = 50, Yoffset As Integer = 20 Dim WithEvents PicCurve As PictureBox Dim xStep As Single = 5, yStep As Single Dim ValueArray As ArrayList Dim ThresHold() As Single 'HIHI、HI、LO、LOLO Dim Name As String = "参数名" Dim Unit As String = "单位" #End Region #Region "构造函数、析构函数" Public Sub New(ByVal mPictureBox As PictureBox, ByVal mThresHold() As Single, ByVal mName As String) ValueArray = New ArrayList PicCurve = mPictureBox ThresHold = mThresHold mPictureBox.BorderStyle = BorderStyle.None Name = mName End Sub Protected Overrides Sub Finalize() MyBase.Finalize() ValueArray.Clear() End Sub #End Region #Region "添加要绘制的点的信息" Public Sub AddValue(ByVal Value As Single) 'Value = ThresHold(0) * Rnd() + ThresHold(3) '测试代码 ValueArray.Add(Value) Call DrawCurve(GetGraphics(PicCurve)) End Sub Public Sub AddValues(ByVal Values() As Single, ByVal G As Graphics) For i As Integer = 0 To Values.Length - 1 ValueArray.Add(Values(i)) Next Call DrawCurve(G) End Sub #End Region #Region "绘制点之间的连线" Private Sub DrawCurve(ByVal G As Graphics, Optional ByVal mClear As Boolean = True) If ValueArray.Count > 0 Then If mClear Then G.Clear(Color.White) G.SmoothingMode = Drawing2D.SmoothingMode.HighQuality Dim mPoints(ValueArray.Count - 1) As Point, Position As Integer = XOffset For i As Integer = ValueArray.Count - 1 To 0 Step -1 Dim ThisValue As Single = CType(ValueArray(i), Single) 'If Position < PicCurve.Width Then Position += xStep Else Exit For Position += xStep mPoints(i) = New Point(Position, (ThresHold(0) - ThisValue) * yStep + Yoffset) Next G.DrawCurve(Pens.Blue, mPoints) mPoints = Nothing End If End Sub #End Region #Region "绘制坐标系统" Private Sub DrawReferenceFrame(ByVal G As Graphics) Dim mPoint1 As New Point, mPoint2 As New Point '定义绘图画笔 Dim MyPen As New Pen(Color.Black, 3) Dim MyStringFormat As New System.Drawing.StringFormat MyStringFormat.Alignment = StringAlignment.Center Dim mSize As New SizeF MyPen.SetLineCap(Drawing2D.LineCap.NoAnchor, Drawing2D.LineCap.ArrowAnchor, Drawing2D.DashCap.Flat) Dim mFont As Font = New Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold) '绘制Y轴 MyPen.Color = Color.Black mPoint1 = New Point(XOffset, PicCurve.Height) mPoint2 = New Point(XOffset, 0) G.DrawLine(MyPen, mPoint1, mPoint2) '绘制X轴 MyPen.Color = Color.LightGreen mPoint1 = New Point(XOffset, PicCurve.Height / 2) mPoint2 = New Point(PicCurve.Width, PicCurve.Height / 2) G.DrawLine(MyPen, mPoint1, mPoint2) '绘制参数名 MyStringFormat.FormatFlags = StringFormatFlags.DirectionVertical mSize = G.MeasureString(Name, mFont) mPoint1.Offset(-mSize.Height, 0) G.DrawString(Name, mFont, Brushes.Black, mPoint1, MyStringFormat) '绘制参数各门限 MyPen.DashStyle = Drawing2D.DashStyle.Dash : MyPen.Width = 2 MyPen.SetLineCap(Drawing2D.LineCap.NoAnchor, Drawing2D.LineCap.NoAnchor, Drawing2D.DashCap.Round) 'HI If ThresHold(1) <> ThresHold(0) Then MyPen.Color = Color.Yellow mPoint1 = New Point(XOffset, (ThresHold(0) - ThresHold(1)) * yStep + Yoffset) mPoint2 = New Point(PicCurve.Width, (ThresHold(0) - ThresHold(1)) * yStep + Yoffset) G.DrawLine(MyPen, mPoint1, mPoint2) mSize = G.MeasureString(ThresHold(1).ToString, mFont) mPoint1.Offset(-mSize.Width, -mSize.Height / 2) G.DrawString(ThresHold(1).ToString, mFont, Brushes.Yellow, mPoint1) End If 'LO If ThresHold(2) <> ThresHold(3) Then mPoint1 = New Point(XOffset, (ThresHold(0) - ThresHold(2)) * yStep + Yoffset) mPoint2 = New Point(PicCurve.Width, (ThresHold(0) - ThresHold(2)) * yStep + Yoffset) G.DrawLine(MyPen, mPoint1, mPoint2) mSize = G.MeasureString(ThresHold(2).ToString, mFont) mPoint1.Offset(-mSize.Width, -mSize.Height / 2) G.DrawString(ThresHold(2).ToString, mFont, Brushes.Yellow, mPoint1) End If 'HIHI MyPen.Color = Color.Red mPoint1 = New Point(XOffset, Yoffset) mPoint2 = New Point(PicCurve.Width, Yoffset) G.DrawLine(MyPen, mPoint1, mPoint2) mSize = G.MeasureString(ThresHold(0).ToString, mFont) mPoint1.Offset(-mSize.Width, -mSize.Height / 2) G.DrawString(ThresHold(0).ToString, mFont, Brushes.Red, mPoint1) 'LOLO mPoint1 = New Point(XOffset, PicCurve.Height - Yoffset) mPoint2 = New Point(PicCurve.Width, PicCurve.Height - Yoffset) G.DrawLine(MyPen, mPoint1, mPoint2) mSize = G.MeasureString(ThresHold(3).ToString, mFont) mPoint1.Offset(-mSize.Width, -mSize.Height / 2) G.DrawString(ThresHold(3).ToString, mFont, Brushes.Red, mPoint1) MyPen.Dispose() mFont.Dispose() End Sub #End Region #Region "要在其上绘制的控件事件" Private Sub PicCurve_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PicCurve.Paint Call DrawReferenceFrame(e.Graphics) End Sub Private Sub PicCurve_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PicCurve.Resize On Error Resume Next xStep = 5 yStep = (PicCurve.Height - 2 * Yoffset) / (ThresHold(0) - ThresHold(3)) End Sub #End Region #Region "绘制永久图像" Function GetGraphics(ByRef pic As PictureBox) As Graphics Dim bmp As Bitmap = New Bitmap(pic.Width, pic.Height) pic.Image = bmp Dim g As System.Drawing.Graphics = Graphics.FromImage(bmp) Return g End Function #End Region #Region "保存图形" Public Sub SaveCurve(ByVal FileName As String) Dim bmp As New Bitmap(PicCurve.Width, PicCurve.Height) Dim g As Graphics = Graphics.FromImage(bmp) Call DrawReferenceFrame(g) Call DrawCurve(g, False) bmp.Save(FileName) End Sub #End Region End Class

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值