ASE Home Page Products Download Purchase Support About ASE
ChartDirector General
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  X and Y value
Posted by jason on Nov-12-2018 13:28
Hi

I'm trying to get x value on click event and store it to text23 .I follow your example but it result error msg,"Object variable or with block variable not set "


Private Sub ChartViewer1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Assume the chart is stored in ChartViewer.Chart
Dim c As XYChart
Set c = ChartViewer1.Chart

'Print out the x value at the clicked position to text23
Dim xValue As Double
xValue = c.getXValue(scaleX(X, Me.ScaleMode, vbPixels))
Text23.Text = xValue



End Sub

what shoud I do ?

  Re: X and Y value
Posted by Peter Kwan on Nov-12-2018 19:57
Hi Jason,

In your charting code, please make sure you use the "Chart" property to display the chart, like:

Set ChartViewer1.Chart = c

In some of our sample code, we use the following method to display the chart:

Set ChartViewer1.Picture = c.makePicture()

With the above method, the "chartViewer.Chart" property is not set, so the line "Set c = ChartViewer1.Chart" will result in "Object variable or with block variable not set " If you change the above to "Set ChartVIewer1.Chart = c", then the "Set c = ChartViewer1.Chart" should work.

Hope this can help.

Regards
Peter Kwan

  Re: X and Y value
Posted by jason on Nov-13-2018 11:05
hi peter

thanks for your reply

I changed my code as follow

Private Sub ChartViewer1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

Dim c As XYChart
Set ChartViewer1.Chart = c
Set c = ChartViewer1.Chart

  'Print out the x value at the clicked position to text23
Dim xValue As Double
xValue = c.getXValue(ScaleX(x, Me.ScaleMode, vbPixels))
Text23.Text = xValue


End Sub

still get same error msg,
actually the line that got error msg is not the " Set c = ChartViewer1.Chart"


but the "xValue = c.getXValue(ScaleX(x, Me.ScaleMode, vbPixels))"

  Re: X and Y value
Posted by Peter Kwan on Nov-13-2018 15:07
Hi Jason,

In your code, it creates a variable "c" that is "Nothing". (In VB6, a newly created object variable is automatically set to "Nothing".) The code then assigns it to ChartViewer.Chart, then read it back. The result is still "Nothing". So it causes the error.

You need to set the ChartViewer1.Chart to the real chart object that your code created. For example, in the vbHelloWorld sample code, the chart is created like the followings:

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
    Dim c As Object
    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

    'include tool tip for the chart
    ChartViewer1.ImageMap = c.getHTMLImageMap("clickable", "", _
        "title='{xLabel}: US${value}K'")

End Sub

Note that in the above code, the "c" is set to the XYChart object created using cd.XYChart(....). The line "Set ChartViewer1.Chart = c" sets the Chart property to the XYChart object.

Then in the mousedown event handler, you can read the XYChart object back:

' The ChartViewer1.Chart contains the actual chart. It is read back into "c".
Dim c As XYChart
Set c = ChartViewer1.Chart

In brief, the ChartViewer1.Chart only contains the chart object you put into it. If you do not put any chart object in ChartViewer1.Chart, then it contains "Nothing". The "c.getXValue(...)" will cause error because you cannot call the getXValue on "Nothing".

Hope this can help.

Regards
Peter Kwan