dcsimg
December 5, 2016
Hot Topics:

Make a Light Chart Control

  • November 19, 2002
  • By Sam Huggill
  • Send Email »
  • More Articles »

The main method is PlotChart, although it is called internally from the Refresh method. Mostly the Line method is used for drawing the graph, and some clever maths calculations are performed to work out where everything goes. The code gets quite involved here, so I wont try and explain how it all works. Take a look at the procedure below and see what you make of it:

Private Sub PlotChart()Dim Columns As LongDim n As Long, i As Long, d As DoubleDim x As Long, y As LongDim x1 As Long, y1 As LongDim PlotTop As Long, PlotBottom As Long, _    PlotLeft As Long, _    PlotRight As LongDim TickString As StringDim LowTick As DoubleDim BarWidth As Intege

n Error GoTo PlotError'determine horizontal extentColumns = UBound(PlotData, 2) + 1'adjust vertical scale if necessaryFor n = 1 To UBound(PlotData) For i = 0 To UBound(PlotData, 2) If m_HighScale < PlotData(n, i) Then m_HighScale = PlotData(n, i) If m_LowScale > PlotData(n, i) Then m_LowScale = PlotData(n, i) Next iNext n'define plot areaPlotLeft = 120 'may be overridden laterPlotRight = ChartWidthPlotTop = TitleOffsetPlotBottom = UserControl.ScaleHeight - (UserControl.TextHeight("X") * 2)'determine vertical tick scaleIf m_LowScale / m_VerticalTickInterval = Int(m_LowScale / m_VerticalTickInterval) Then LowTick = m_LowScaleElse LowTick = Int(m_LowScale / m_VerticalTickInterval) * m_VerticalTickIntervalEnd If'determine left spacing'check vertical captionsFor d = LowTick To HighScale Step m_VerticalTickInterval If PlotLeft < (UserControl.TextWidth(Format$(d)) + 120) Then PlotLeft = (UserControl.TextWidth(Format$(d)) + 120) End IfNext d'check caption for first horizontal tickIf PlotLeft < (UserControl.TextWidth(PlotData(0, 0)) / 2) + 60 Then PlotLeft = (UserControl.TextWidth(PlotData(0, 0)) / 2) + 60End If'draw row ticksFor d = LowTick To HighScale Step m_VerticalTickInterval y = PlotBottom - (PlotBottom - PlotTop) * ((d - LowTick) / (m_HighScale - LowTick)) UserControl.Line (PlotLeft, y)-Step(60, 0) UserControl.CurrentX = PlotLeft - (UserControl.TextWidth(Format$(d)) + 60) UserControl.CurrentY = y - (UserControl.TextHeight("X") * 0.5) UserControl.Print Format$(d)Next d'draw plot boxUserControl.Line (PlotLeft, PlotTop)-(PlotRight, PlotBottom), , B'draw column ticks and captionsFor i = 0 To Columns - 1 Step m_HorizontalTickFrequency x = PlotLeft + (((PlotRight - PlotLeft) / (Columns - 1)) * i) UserControl.Line (x, PlotBottom)-Step(0, -60) UserControl.CurrentX = x - (UserControl.TextWidth(PlotData(0, i)) / 2) UserControl.CurrentY = PlotBottom + (UserControl.TextHeight("X") * 0.5) UserControl.Print PlotData(0, i)Next i'base barwidth on series and pointsIf m_ChartType = Bar Then BarWidth = (PlotRight - (PlotLeft + 60)) / (Columns * UBound(PlotData)) - 30 If BarWidth <= 15 Then BarWidth = 30End If'plot graphFor n = 1 To UBound(PlotData) For i = 0 To UBound(PlotData, 2) 'determine coordinates x = PlotLeft + (((PlotRight - PlotLeft) / (Columns - 1)) * i) - 15 If PlotData(n, i) = LowTick Then y = PlotBottom - 15 Else y = (PlotBottom - ((PlotData(n, i) - LowTick) / (m_HighScale - LowTick) _ * (PlotBottom - PlotTop))) - 15 End If Select Case m_ChartType Case Bar 'adjust x for series x = PlotLeft + (((PlotRight - PlotLeft) / Columns) * i) - 15 x = x + 30 + ((n - 1) * (BarWidth + 30)) UserControl.Line (x, y)-(x + BarWidth, PlotBottom - 15), PlotColors(n - 1), BF Case Line 'draw data point If m_PlotPoints Then UserControl.Line (x, y)-Step(30, 30), PlotColors(n - 1), BF End If 'draw data graph If i <> 0 Then UserControl.Line (x + 15, y + 15)-(x1 + 15, y1 + 15), PlotColors(n - 1) End If x1 = x y1 = y End Select Next iNext nPlotExit: Exit Sub PlotError: If Err = 9 Then 'fail silently, not initialized yet Else Err.Raise 32007, "GraphLiteProject.GraphLite", _ "Error " & Err & " plotting graph: " & Error$(Err) End If Resume PlotExit End Sub

Gets rather confusing eh? Oh well, the DrawTitle method shouldnt be too bad. Basically after the position of the text has been calculated using the ScaleWidth and TextWidth properties, all thats left to do is to use the Print function to apply the text:

Private Sub DrawTitle()    Dim f As Font    Set f = UserControl.Font    Set UserControl.Font = m_TitleFont    UserControl.CurrentX = (UserControl.ScaleWidth - UserControl.TextWidth(m_Title)) / 2    UserControl.Print m_Title    TitleOffset = UserControl.CurrentY    Set UserControl.Font = fEnd Sub

The DrawLegend method is a bit more complicated, although it works on much the same principle of calculating the positions, and using the Line and Print methods to apply the data.





Page 6 of 7



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel