|
xAxis().addMark after |
Posted by Jean-Luc on Mar-04-2016 22:29 |
|
Hi Peter,
I'm using VB6 and CD 6.0, attempting to add marks after the chart is produced.
Below is my code, can you help me understand why the button fails to add a second vertical mark?
How should I proceed?
Thanks in advance!
Option Explicit
Dim cd As New ChartDirector.API
Dim c As Object
Dim theMark As Object
Private Sub Form_Load()
Set c = cd.XYChart(250, 250) 'Create a XYChart object of size 250 x 250 pixels
Dim data(): data = Array(85, 156, 179.5, 211, 123) 'The data for the bar chart
Dim labels(): labels = Array("Mon", "Tue", "Wed", "Thu", "Fri") 'The labels for the bar chart
Call c.setPlotArea(30, 20, 200, 200) 'Set the plotarea at (30, 20) and of size 200 x 200 pixels
Call c.addBarLayer(data) 'Add a bar chart layer using the given data
Call c.xAxis().setLabels(labels) 'Set the x axis labels using the given labels
Set theMark = c.xAxis().addMark(2, &HFF) 'add a blue vertical line at 3rd bar
Set ChartViewer1.Picture = c.makePicture() 'output the chart
End Sub
Private Sub Command1_Click()
Set theMark = c.xAxis().addMark(3, &HFF) 'should add a vertical line at 4th bar
Set ChartViewer1.Picture = Nothing 'clear the chart
MsgBox ("click to redraw the chart")
Set ChartViewer1.Picture = c.makePicture() 'output the chart
End Sub |
Re: xAxis().addMark after |
Posted by Peter Kwan on Mar-05-2016 01:04 |
|
Hi Jean-Luc,
In ChartDirector, the chart cannot be changed after drawing (after makePicture) except by using the "dynamic layer". For your case, a better method is as follows, which allows your code to add marks, remove marks, move marks, etc.., by simply updating the marks array, and without the need to modify the charting code.
Option Explicit
Dim marks()
Private Sub Form_Load()
ReDim marks(0)
marks(0) = 2
drawChart
End Sub
Private Sub Command1_Click()
ReDim Preserve marks(UBound(marks) + 1)
marks(UBound(marks)) = 3
drawChart
End Sub
Private Sub drawChart()
Dim cd As New ChartDirector.API
Dim c As XYChart
Set c = cd.XYChart(250, 250) 'Create a XYChart object of size 250 x 250 pixels
Dim data(): data = Array(85, 156, 179.5, 211, 123) 'The data for the bar chart
Dim labels(): labels = Array("Mon", "Tue", "Wed", "Thu", "Fri") 'The labels for the bar chart
Call c.setPlotArea(30, 20, 200, 200) 'Set the plotarea at (30, 20) and of size 200 x 200 pixels
Call c.addBarLayer(data) 'Add a bar chart layer using the given data
Call c.xAxis().setLabels(labels) 'Set the x axis labels using the given labels
Dim i As Long
For i = 0 To Ubound(marks)
If marks(i) <> cd.NoValue Then Call c.xAxis().addMark(marks(i), &HFF)
Next
Set ChartViewer1.Picture = c.makePicture() 'output the chart
End Sub
Hope this can help.
Regards
Peter Kwan |
Re: xAxis().addMark after |
Posted by Peter Kwan on Mar-08-2016 01:33 |
|
Hi Jean-Luc,
The Programming Track Cursor sample code and the supporting ChartViewer methods have not been ported to VB6, as VB6 lacks certain essentially events (eg. in VB6, there is no "mouse out" events). However, the core features (like the "dynamic layer" are actually available. We just need to more code to work around the VB6 limitations.
Below please find an example modified from the "Hello World" VB6 sample code included in "ChartDirector for ASP/COM/VB". Basically, in the mouse move event, we convert the VB TWIPS coordinates to pixel coordinates, and trigger the ViewPortChanged event. In the ViewPortChanged event handler, the dynamic layer is created and a vertical line is drawn at the mouse pixel coordinate.
In this example, the line will remain at its last position when the mouse move away from the chart. You can modify it to hide the line or to lock the line if a button is pressed by the keyword. (To lock the line, just sets a flag to cause the Mouse Move event to do nothing.) The chart can be saved by using c.makeTmpFile.
Hope this can help.
Regards
Peter Kwan
===============================================
Option Explicit
Private c As XYChart
Private mouseX As Long
Private mouseY As Long
Private Sub ChartViewer1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
mouseX = Me.ScaleX(X, Me.ScaleMode, vbPixels)
mouseY = Me.ScaleY(Y, Me.ScaleMode, vbPixels)
Call ChartViewer1.updateViewPort(True, True)
End Sub
Private Sub ChartViewer1_ViewPortChanged(needUpdateChart As Boolean, needUpdateImageMap As Boolean)
Dim d As DrawArea
Set d = c.initDynamicLayer()
If mouseX > c.getPlotArea().getRightX() Then mouseX = c.getPlotArea().getRightX()
If mouseX < c.getPlotArea().getLeftX() Then mouseX = c.getPlotArea().getLeftX()
Call d.vline(c.getPlotArea().getTopY(), c.getPlotArea().getBottomY(), mouseX, &HFF)
Set ChartViewer1.Chart = c
End Sub
Private Sub Form_Load()
Dim cd As New ChartDirector.API
'The data for the bar chart
Dim data()
data = Array(85, 156, 179.5, 211, 123)
'The labels for the bar chart
Dim labels()
labels = Array("Mon", "Tue", "Wed", "Thu", "Fri")
'Create a XYChart object of size 250 x 250 pixels
Set c = cd.XYChart(250, 250)
'Set the plotarea at (30, 20) and of size 200 x 200 pixels
Call c.setPlotArea(30, 20, 200, 200)
'Add a bar chart layer using the given data
Call c.addBarLayer(data)
'Set the x axis labels using the given labels
Call c.xAxis().setLabels(labels)
'output the chart
Set ChartViewer1.Chart = c
End Sub |
Re: xAxis().addMark after |
Posted by Jean-Luc on Mar-08-2016 23:29 |
|
OK, works great Peter!
For people reading this post in the future, below is my implementation for this "TrackLine on FinanceChart in VB6".
In fact I'm using VBA, thus:
-The VB6 functions around twips & pixels are not available,
-The known fTwipsToPixels function using APIs doesn't give satisfaction in this case, still to understand why...
So, just after implementing 4 XYcharts in my FinanceChart, I compute once:
TrackLineHeight = the sum of all (TheXYChart.getPlotArea().getBottomY() - TheXYChart.getPlotArea().getTopY())
Then just the code below and I'm done:
Private mouseX1 As Long, mouseX2 As Long, TrackLineHeight As Long
Private Sub ChartViewer1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case Shift 'Shift = 1 for Shift, 2 for CTRL and 4 for ALT
Case Is < 2
Exit Sub
Case Is = 2
mouseX1 = X * 4 / 3
Case Is = 4
mouseX2 = X * 4 / 3
End Select
Call ChartViewer1.updateViewPort(True, True)
End Sub
Private Sub ChartViewer1_ViewPortChanged(needUpdateChart As Boolean, needUpdateImageMap As Boolean)
Dim d As DrawArea
Set d = c.initDynamicLayer() 'c is a FinanceChart
Call d.vline(0, TrackLineHeight, mouseX1, &HFF)
Call d.vline(0, TrackLineHeight, mouseX2, &HFF8800)
Set ChartViewer1.Chart = c
End Sub
_________________________________________________________________________
With this I can have a blue line if pressing CTRL key while positionning the mouse,
and in addition an orange line if pressing ALT key while positionning the mouse.
No longer want one these lines? just drag it on the side, then it can't be seen.
I'm very happy, once again a thousand thanks Peter!!
Jean-Luc |
|