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

Message ListMessage List     Post MessagePost Message

  Chart Auto Sizing to Windows control
Posted by KeithB on Feb-20-2024 02:54
Is there any particular reason you don't have the examples autoscale to winChartViewer1.Size.Height and Width? Why do you always just create the chart and plot area manually?

Also, is there a way to create placeholder images so that you don't get a huge blank space when the program starts up? Much like the way the old Forms Data Visualization chart put a fake chart up as a default for the form.

  Re: Chart Auto Sizing to Windows control
Posted by Peter Kwan on Feb-21-2024 02:33
Hi KeithB,

By default, the WinChartViewer will auto-size to the chart size. For example, if the chart is 600 x 400, the WinChartViewer will auto-size to 600 x 400.

The following is an example of a chart that auto-sizes to fit the window. It simply sets the chart size to the same size of the WinChartViewer.

https://www.advsofteng.com/tutorials/real_time_sweep_chart/real_time_sweep_chart.html

Note that in the above example, we put the y-axis labels and the legend box inside the plot area. This allows the plot area to be larger. The x-axis, being quite thin, is still outside the plot area. This illustrates even an auto-sized chart still needs to be configured. As ChartDirector supports a lot of chart types with very flexible layout, it needs an API to configure the chart.

For the placeholder images, you can put a chart in the WinChartViewer in the "Load" event of your Form. In this way, when the Form appears, you will see the chart. You can use one of the sample code as the placeholder if you like. If you want to display an image, you can create a blank chart with just a background image of your choice.

Best Regards
Peter Kwan

  Re: Chart Auto Sizing to Windows control
Posted by KeithB on Feb-22-2024 02:39
Thanks.
What is the relationship between the Chart and the Viewer?

I am trying to create some interactive scaling so I have forms code like this:

(The chart has already been built and displayed)

private void numMaxY_ValueChanged(object sender, EventArgs e)
{
    if (ChartXY == null) return;

    if (numMaxY.Value == 0)
    {
        ChartXY.yAxis().setLinearScale3();
    }
    else
    {
        ChartXY.yAxis().setLinearScale(0, (double)numMaxY.Value);
    }

    // Output the chart
    winChartViewer1.Chart = ChartXY;

    //include tool tip for the chart
    winChartViewer1.ImageMap = ChartXY.getHTMLImageMap("clickable", "",
        "title='{dataSetName}:{x}, {value}'");
}

where ChartXY is a global chart object that I used to build the chart. But if I run this code, the changes I made to the chart don't show up when I re-assign it to the WinViewer.

  Re: Chart Auto Sizing to Windows control
Posted by Peter Kwan on Feb-22-2024 22:50
Hi KeithB,

In ChartDirector, the BaseChart object (ChartXY in your code) cannot be changed once it has been displayed. So changing it in your code has no effect.

To interactively change something, in the simplest case, you just need to draw the chart again.

private void numMaxY_ValueChanged(object sender, EventArgs e)
{
      drawChart();
}

In the above, drawChart is the method that you use to draw and display the chart. It should draw the chart according to the configuration of your user. That's means it should have some lines that supports numMaxY, like:

    // Put these code in drawChart
    if (numMaxY.Value == 0)
    {
        ChartXY.yAxis().setLinearScale3();
    }
    else
    {
        ChartXY.yAxis().setLinearScale(0, (double)numMaxY.Value);
    }

There are several examples in ChartDirector that allows the user to change the chart. For example, consider the "Interactive Financial Chart" sample code:

https://www.advsofteng.com/doc/cdnet.htm#financedemo.htm

In that example, there are many controls for the user to change the chart. Most of them just call the following event handler:

private void selectionChanged(object sender, System.EventArgs e)
{
     if (hasFinishedInitialization)
         drawChart(winChartViewer1);
}

The event handler just called drawChart to redraw the chart. We add a hasFinishedInitialization flag because in Windows Forms, the changed event can occur during initialization before the window is displayed. We make sure the drawChart is called only after the window has been displayed.  (During initialization, the code can set up the initial state of the control. In Windows Forms, this can trigger the changed event.)

If your code supports zooming and scrolling or real time updates, normally it has a ViewPortChanged event handler. In this case, instead of drawChart, you use WinChartViewer.updateViewPort instead. The following is an example:

https://www.advsofteng.com/doc/cdnet.htm#zoomscrolltrack2.htm

In the above example, all the check boxes are handled by:

private void alphaCB_CheckedChanged(object sender, EventArgs e)
{
     winChartViewer1.updateViewPort(true, true);
}

Best Regards
Peter Kwan

  Re: Chart Auto Sizing to Windows control
Posted by KeithB on Feb-22-2024 22:59
This is obviously what happens, but thanks for confirming it. I am used to MSChart where the object is "live" and changing things automagically changes the view.

I was concerned about performance, but when you think about it, MSChart has to do the same thing, it just does it behind the scenes.

I guess what I don't understand, is why I have to create a new chart object. for example, this does not work:

XYChart c = new XYChart()
// chart stuff

winChartViewer.Chart = c; // chart is displayed!

c.MakeAChange()


winChartViewer.Chart = c; // Chart is not displayed!

It seems the only thing you can do with a chart after the fact is save the image.

  Re: Chart Auto Sizing to Windows control
Posted by Peter Kwan on Feb-22-2024 23:55
Hi KeithB,

When you draw a pixel, it overwrites the old pixel and it is lost forever. When you move an object, you cannot know what is revealed under that object as that information is lost. That's why we need to redraw everything from bottom up.

In ChartDirector, you can only do limited things after displaying the chart. You can add a "dynamic layer" and draw on the dynamic layer and remove the dynamic layer later. This is how the track cursor is implemented in ChartDirector.

Without using the dynamic layer, you can use BaseChart.addText to add text, shapes or images to the chart (as ChartDirector supports CDML, the text can also include shapes and images), and to use the DrawArea object to draw complex things on the chart, but you cannot move or remove things. To update a chart object that is already in the WinChartViewer, you can call "viewer.updateDisplay();". Basically, this tells ChartDirector your code has made all the necessary changes and it is OK to update the display. This API is used in the various track cursor sample code.

In terms of speed, we think ChartDirector is quite fast. It is often used for real time charts.
The drawChart code only configures the chart. The CPU it needs is negligible compared to actually drawing the chart pixel by pixel, which is required even for MSChart.

Best Regards
Peter Kwan