ClassDrawChart类(画柱状图)

原创 2004年09月20日 10:04:00

Imports System.Drawing.Drawing2D
Imports System.Drawing
'绘制图表
Public Class ClassDrawChart

#Region "字段"
    Private mData As ArrayList                                                      '数据
    Private mItemName As ArrayList                                                  '检测项目名称
    Private mintCount As Integer                                                    '数据个数
    Private mintAllTestCount As Integer                                             '总检测次数
    Private mType As ClassTest.TestType                                             '类型
    Private mBmp As Bitmap                                                          '图形
    Private mImageStream As System.IO.MemoryStream                                  '图形流  
    Private mintWidth As Integer = 1080                                             '宽
    Private mintHeight As Integer = 740                                             '高
    Private GDI As Graphics                                                         'GDI
#End Region

#Region "属性"
    Public ReadOnly Property Image() As Bitmap                                      '图形
        Get
            Return mBmp
        End Get
    End Property

    Public ReadOnly Property ImageStream() As System.IO.MemoryStream                '图形流
        Get
            Return mImageStream
        End Get
    End Property
#End Region

#Region "方法"
    Public Sub New(ByVal Data As ArrayList, ByVal ItemName As ArrayList, ByVal Type As ClassTest.TestType, ByVal AllTestCoun As Integer)       '构造函数
        mData = Data
        mItemName = ItemName
        mintCount = Data.Count
        mType = Type
        mintAllTestCount = AllTestCoun
    End Sub                        '构造
    Public Sub Drawing()
        Try
            '绘制
            mBmp = New Bitmap(mintWidth, mintHeight)
            mImageStream = New System.IO.MemoryStream
            GDI = Graphics.FromImage(mBmp)
            GDI.Clear(Color.White)
            '绘制标题
            DrawingTitle(mType, GDI)
            '绘制坐标轴
            DrawingXYAxis(mType, GDI)
            '绘制柱状图
            DrawingChart(mType, GDI)
            '保存图表至内存中
            mBmp.Save(mImageStream, Imaging.ImageFormat.Bmp)
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
            ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_Drawing")
        End Try
    End Sub                                                                             '绘制
    Private Sub DrawingTitle(ByVal Type As ClassTest.TestType, ByVal GDI As Graphics)
        Try
            '绘制标题
            Dim Title As String
            Dim TitleFont As New Font("宋体", 12, FontStyle.Bold)
            Dim X, Y As Integer
            Select Case Type
                Case ClassTest.TestType.CarComputer
                    Title = "车身电脑不合格统计图表"
                Case ClassTest.TestType.CompositeMeter
                    Title = "组合仪表不合格统计图表"
            End Select
            X = (mintWidth - TitleFont.Size * Title.Length * 1.4) / 2
            Y = 22
            GDI.DrawString(Title, TitleFont, New SolidBrush(Color.Black), X, Y)
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
            ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_DrawingTitle")
        End Try
    End Sub                '绘制标题
    Private Sub DrawingXYAxis(ByVal Type As ClassTest.TestType, ByVal GDI As Graphics)
        Try
            '绘制坐标轴
            Dim XAxis_X1, XAxis_Y1, XAxis_X2, XAxis_Y2 As Integer                   'X轴坐标
            Dim YAxis_X1, YAxis_Y1, YAxis_X2, YAxis_Y2 As Integer                   'Y轴坐标
            Dim Point_1, Point_2, Point_3 As Point                                  '三角坐标
            Dim TrigonPoint As Point()                                              '三角坐标系
            Dim TrigonBrush As New SolidBrush(Color.Black)                          '笔刷
            Dim LinePen As New Pen(Color.Black, 2)
            XAxis_X1 = 20
            XAxis_X2 = mintWidth - 20
            XAxis_Y1 = mintHeight - 50
            XAxis_Y2 = XAxis_Y1
            GDI.DrawLine(LinePen, XAxis_X1, XAxis_Y1, XAxis_X2, XAxis_Y2)           'X轴
            Point_1 = New Point(XAxis_X2, XAxis_Y1)
            Point_2 = New Point(XAxis_X2 - 15, XAxis_Y1 - 5)
            Point_3 = New Point(XAxis_X2 - 15, XAxis_Y1 + 5)
            TrigonPoint = New Point() {Point_1, Point_2, Point_3}
            GDI.FillPolygon(TrigonBrush, TrigonPoint)                               'X轴三角
            YAxis_X1 = 40
            YAxis_X2 = YAxis_X1
            YAxis_Y1 = 50
            YAxis_Y2 = mintHeight - 30
            GDI.DrawLine(LinePen, YAxis_X1, YAxis_Y1, YAxis_X2, YAxis_Y2)           'Y轴
            Point_1 = New Point(YAxis_X1, YAxis_Y1)
            Point_2 = New Point(YAxis_X1 - 5, YAxis_Y1 + 15)
            Point_3 = New Point(YAxis_X1 + 5, YAxis_Y1 + 15)
            TrigonPoint = New Point() {Point_1, Point_2, Point_3}
            GDI.FillPolygon(TrigonBrush, TrigonPoint)                               'X轴三角
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
            ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_DrawingXYAxis")
        End Try
    End Sub               '绘制坐标轴
    Private Sub DrawingChart(ByVal type As ClassTest.TestType, ByVal GDI As Graphics)
        '绘制柱状图
        Try
            Dim PerWidth, PerHeight, AllWidth, AllHeight As Integer
            Dim iLoop As Integer
            Dim X1, Y1, X2, Y2, X, Y, Width, Height As Integer
            Dim RectangleBrush As New HatchBrush(HatchStyle.BackwardDiagonal, Color.Red, Color.White)
            Dim RectanglePen As New Pen(Color.Red)
            Dim DataFont As New Font("宋体", 9)
            Dim TitleFont As New Font("宋体", 9)
            Dim TitleDrawFormat As New StringFormat
            AllWidth = mintWidth - 40 - 20 - 15 - 20                                   '绘制柱状图的Y轴长度
            AllHeight = mintHeight - 50 - 15 - 20 - 30 - 20
            X1 = 40
            Y1 = mintHeight - 50
            X2 = mintWidth - 20 - 15 - 20
            Y2 = Y1
            PerWidth = (X2 - X1) / mintCount
            PerHeight = AllHeight / mintAllTestCount
            Width = PerWidth / 3
            '绘制
            For iLoop = 0 To mintCount - 1
                '绘制柱状图
                If mData(iLoop) = 0 Then
                    X = X1 + (PerWidth * 2 / 3) + (PerWidth * iLoop)
                    Y = Y2
                    Height = 10
                    GDI.FillRectangle(RectangleBrush, X, Y, Width, Height)
                    GDI.DrawRectangle(RectanglePen, X, Y, Width, Height)
                Else
                    X = X1 + (PerWidth * 2 / 3) + (PerWidth * iLoop)
                    Y = Y2 - (PerHeight * mData(iLoop))
                    Height = PerHeight * mData(iLoop)
                    GDI.FillRectangle(RectangleBrush, X, Y, Width, Height)
                    GDI.DrawRectangle(RectanglePen, X, Y, Width, Height)
                End If
                '绘制数字
                Select Case mData(iLoop).ToString.Length
                    Case 1                                  '1位数字
                        GDI.DrawString(mData(iLoop), DataFont, New SolidBrush(Color.Red), X, Y - DataFont.Height)
                    Case 2                                  '2位数字
                        GDI.DrawString(mData(iLoop), DataFont, New SolidBrush(Color.Red), X - DataFont.Size / 2, Y - DataFont.Height)
                    Case 3                                  '3位数字
                        GDI.DrawString(mData(iLoop), DataFont, New SolidBrush(Color.Red), X - DataFont.Size * 2 / 3, Y - DataFont.Height)
                End Select
                '绘制名称
                TitleDrawFormat.FormatFlags = StringFormatFlags.DirectionVertical
                GDI.DrawString(mItemName(iLoop), TitleFont, New SolidBrush(Color.Blue), (X1 + (PerWidth * iLoop)), (Y1 - mItemName(iLoop).ToString.Length * TitleFont.Height), TitleDrawFormat)
            Next
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
            ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_DrawingChart")
        End Try
    End Sub                '绘制柱状图
    Public Sub Print(ByVal GDI As Graphics)
        '打印
        Try
            GDI.DrawImage(mBmp, 0, 0)
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
            ClassComm.WriteErrMessage(ex.ToString, "ClassDrawChart_Print")
        End Try
    End Sub                                                          '打印图表
#End Region

End Class

C#绘制柱状图

public Image GetBarChart(string title,int width,int height,string ToolNo,int RateLife,int RealLife) ...
  • pengfeixiong
  • pengfeixiong
  • 2012年08月29日 12:56
  • 2836

Python——使用matplotlib绘制柱状图

Python——使用matplotlib绘制柱状图 1、基本柱状图           首先要安装matplotlib 可以使用pip命令直接安装 # -*- coding: utf-8 -*- im...
  • qq_29721419
  • qq_29721419
  • 2017年05月11日 17:05
  • 5750

R语言做柱状图

R语言做柱状图 转自:http://www.phperz.com/article/16/0102/180120.html 条形图代表在与条成比例的变量的值的长度矩形条数据。R使用函数bar...
  • g863402758
  • g863402758
  • 2016年11月29日 10:15
  • 5047

Android中利用Canvas画简单柱状图

本人是一个新手 ,今天第一次写博客,然后突然看到Canvas画图,然后就用几分钟时间写了一个特别垃圾的柱状图, 用到的代码没有一点技术含量,更适合新手看。下面贴代码。       主要代码类,里面...
  • AndroidFei
  • AndroidFei
  • 2016年07月15日 14:14
  • 1035

Python:Matplotlib 画曲线和柱状图(Code)

这是我关于pose识别率的实验结果,感觉结果真是令人不可思议!(非博主原文!) 原文链接:http://blog.csdn.net/ikerpeng/article/details/20523679 ...
  • wishchin
  • wishchin
  • 2014年05月03日 15:39
  • 30288

Matlab画柱状图和折线图的暗黑技巧

Matlab有很强的绘图功能,其基本的用法参阅帮助文档已经足矣。但是对于一些定制性的功能,在帮助文档中未必能很容易查到,或者说是隐(暗)藏(黑)技巧。本文将介绍柱状图(或条形图)和折线图是会用到的一些...
  • baimafujinji
  • baimafujinji
  • 2016年10月05日 12:38
  • 4369

java 画柱状图的总结

JAVA绘制柱状图方法总结 使用库文件:jreechar-1.0.13 jcommon1.0.16 下载地址:https://cn.jarfire.org/jcommon.html ht...
  • laoyangkidd
  • laoyangkidd
  • 2015年02月06日 08:46
  • 1796

基于matlab的三维柱状图绘制

基于matlab的三维柱状图绘制 摘要: 对于生物建模、数据统计等,经常会得到三维数据,绘制三维柱状图是显示它们的重要方式。但matlab绘制三维柱状图,可能需要进一步的人工干预...
  • u014114990
  • u014114990
  • 2015年11月27日 15:55
  • 3928

用canvas画的柱状图

很开心跟大家一起分享。
  • weixin_35783350
  • weixin_35783350
  • 2016年09月18日 08:38
  • 2909

MATLAB画柱状图填充(多种)

1、把下列代码保存为名为“applyhatch.m”的文件 function applyhatch(h,patterns,colorlist) %APPLYHATCH Apply hatched ...
  • WSN_IPv6
  • WSN_IPv6
  • 2016年12月05日 15:38
  • 2616
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ClassDrawChart类(画柱状图)
举报原因:
原因补充:

(最多只允许输入30个字)