Saving charts with dynamic view

2020-04-21 06:17发布

In my WinForms application, I have to display line graphs.

One feature which I need to implement in my application is saving the chart to a file. Once the chart is saved and the application is closed, the saved chart can be opened and the user can zoom-in/zoom-out the chart for analysis.

Most charting tools enable user to save the chart to an image file like .png/.jpg etc., which are static.

I want to achieve something like Matlab's FIG file. Are there controls which already do this? Or if I have to implement it myself, what is the best approach?

1条回答
欢心
2楼-- · 2020-04-21 07:04

You have a couple of options:

  • Save as Jpg or Png in a size large enough to zoom in. To do so you would enlarge the chart before saving and then shrink it again.

  • Save in one of the emf formats. This saves a vector format, so zooming in works very well, even at large zoom factors. ((erroneous rant omitted))

  • Save to XML. This means serializing, either using the standard options or by marking certain properties as serializable or non-serializable. See here for more info! This code is straight from the link:

    string yourChartDataFile = "d:\\SavedChartData.xml";
    
    private void saveButton_Click(object sender, EventArgs e)
    {
        chart1.Serializer.Save(yourChartDataFile);
    }
    
    private void loadButton_Click(object sender, EventArgs e)
    {
        chart1.Serializer.Load(yourChartDataFile);
    }
    

To display the resulting file you need another chart control, obviously.

I found the two-liner above to work pretty well without adding frills, but you may need or want to save things like current zoom state or annotation states; I'm not sure just what does get serialized out of the box and what does not..

Update Actually you can view and work with emf by loading them into a Metafile, which can be loaded into a PictureBox or drawn in a suitable size with Graphics.DrawImage..

查看更多
登录 后发表回答