Save chart to memory stream ver, 4.1.2015.12163

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
imd-soft
Newbie
Newbie
Posts: 11
Joined: Tue Mar 29, 2016 12:00 am

Save chart to memory stream ver, 4.1.2015.12163

Post by imd-soft » Thu May 04, 2017 8:18 am

Hi.
We have an error in the TeeChart.WPF.dll (Height must be non-negative) in our project
when we are trying save the chart to memory stream.
Can you help with it?

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Save chart to memory stream ver, 4.1.2015.12163

Post by Christopher » Thu May 04, 2017 9:54 am

Hello,

the following code works as expected:

Code: Select all

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      CreateChart();
      InitializeChart(); 
    }

    TChart tChart1, tChart2;

    private void InitializeChart()
    {
      Line line = new Line(tChart1.Chart);
      line.FillSampleValues();
    }

    private void CreateChart()
    {
      tChart1 = new TChart();
      tChart1.Margin = new Thickness(0);
      tChart1.Height = 200;
      dockPanel.Children.Add(tChart1);

      tChart2 = new TChart();
      tChart2.Margin = new Thickness(0);
      tChart2.Height = 200;
      dockPanel.Children.Add(tChart2);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      MemoryStream stream = new MemoryStream();
      tChart1.Export.Template.Save(stream);

      stream.Position = 0;
      tChart2.Import.Template.Load(stream);
    }
  }
could you please modify the above code so I can reproduce your issue here?
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

imd-soft
Newbie
Newbie
Posts: 11
Joined: Tue Mar 29, 2016 12:00 am

Re: Save chart to memory stream ver, 4.1.2015.12163

Post by imd-soft » Thu May 04, 2017 10:27 am

I am sending the code with an error.

MainWindow.xaml
<Window x:Class="TeeChart.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006 ... esentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/marku ... ility/2006"
xmlns:local="clr-namespace:TeeChart"
xmlns:wpf="clr-namespace:Steema.TeeChart.WPF;assembly=TeeChart.WPF"
mc:Ignorable="d"
Title="MainWindow" Height="500" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<wpf:TChart Name="tChart" Grid.Row="0"/>
<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
<Button x:Name="BtnGetImage" Click="BtnGetImage_OnClick" Margin="5">Save chart to memory stream</Button>
</StackPanel>
</Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += MainWindow_Loaded;
}

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
tChart.Series.Clear();
tChart.Axes.Custom.Clear();

tChart.Width = 731;
tChart.Height = 448;
tChart.Axes.Bottom.SetMinMax(0, 12);

AddFirstTwoSeries();

var mySeries = SetMySeries();


SetDataMySeries(mySeries);

AddCustomAxes(mySeries);
}

private void AddCustomAxes(Series mySeries)
{
Steema.TeeChart.WPF.Axis newAxis = new Steema.TeeChart.WPF.Axis();
newAxis.Horizontal = false;
Steema.TeeChart.WPF.Axis customAxis = tChart.Axes.Custom.Add(newAxis);

mySeries.CustomVertAxis = customAxis;
var customAxisId = tChart.Axes.Custom.IndexOf(customAxis);

var myCustomAxis = tChart.Axes.Custom[customAxisId];
myCustomAxis.Visible = true;
myCustomAxis.AxisPen.Color = mySeries.Color;
myCustomAxis.MinorGrid.Visible = false;
myCustomAxis.Labels.Font.Color = mySeries.Color;
myCustomAxis.Labels.Font.Bold = false; //True
myCustomAxis.Ticks.Visible = false;
myCustomAxis.MinorTicks.Visible = false;
myCustomAxis.MinorGrid.Visible = false;
myCustomAxis.Grid.Visible = false;
myCustomAxis.AxisPen.Visible = false;
myCustomAxis.Increment = 1;

myCustomAxis.Labels.ValueFormat = "###0.###";

myCustomAxis.Automatic = false;
myCustomAxis.Maximum = (double) 10;
myCustomAxis.Minimum = (double) 0;

myCustomAxis.PositionUnits = Steema.TeeChart.WPF.PositionUnits.Percent;

myCustomAxis.RelativePosition = 0;
}

private static void SetDataMySeries(Series mySeries)
{
mySeries.Title = "Di";
mySeries.Legend.Visible = true;

mySeries.Add(2, (double)2, " 1", Colors.Blue);
mySeries.Add(3, (double)12, " 2", Colors.Blue);
}

private Series SetMySeries()
{
Steema.TeeChart.WPF.Styles.Series series3 = tChart.Series.Add(new Steema.TeeChart.WPF.Styles.Line(tChart.Chart));
var seriesIndex = tChart.Series.IndexOf(series3);

var mySeries = tChart.Series[seriesIndex];
((Steema.TeeChart.WPF.Styles.Line) mySeries).Pointer.Style =
(Steema.TeeChart.WPF.Styles.PointerStyles) 0;
((Steema.TeeChart.WPF.Styles.Line) mySeries).Pointer.Visible = true;

((Steema.TeeChart.WPF.Styles.Line) mySeries).LinePen.Visible = false;
return mySeries;
}

private void AddFirstTwoSeries()
{
tChart.Series.Add(new Steema.TeeChart.WPF.Styles.Line(tChart.Chart));

tChart.Series[0].VertAxis = Steema.TeeChart.WPF.Styles.VerticalAxis.Left;
tChart.Series[0].Legend.Visible = false;

tChart.Series.Add(new Steema.TeeChart.WPF.Styles.Line(tChart.Chart));
tChart.Series[1].VertAxis = Steema.TeeChart.WPF.Styles.VerticalAxis.Right;
tChart.Series[1].Legend.Visible = false;
}

private void BtnGetImage_OnClick(object sender, RoutedEventArgs e)
{
var xamlFormat = new XAMLFormat(tChart.Chart);
using (MemoryStream ms = new MemoryStream())
{
try
{
xamlFormat.Save(ms);
MessageBox.Show("Ok", "Saving chart to memory stream");
}
catch (Exception ex)
{
Console.WriteLine(ex);
MessageBox.Show($" {ex.Message}", "Error when saving chart to memory stream ");
}
}
}
}

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Save chart to memory stream ver, 4.1.2015.12163

Post by Christopher » Thu May 04, 2017 2:56 pm

Hello,

Thank you. I have been able to reproduce this error and have added it to our issue-tracking software with id=1857. As you can see, this issue has now been fixed.

A workaround is to make sure that there are no points outside of the minimum and maximum axes ranges when exporting to XAML. In your code example you could do this by adding in a loop to delete the points outside of the maximum left axis range in the AddCustomAxes method, e.g.

Code: Select all

      myCustomAxis.Automatic = false;
      myCustomAxis.Maximum = (double)10;
      myCustomAxis.Minimum = (double)0;

      for (int i = 0; i < mySeries.Count; i++)
      {
        if(mySeries.YValues[i] > myCustomAxis.Maximum)
        {
          mySeries.Delete(i);
        }
      }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply