none
How to add a Chart in Flow document. RRS feed

  • Question

  • hi,

    i need to add a wpf Chart to a flow document for generating XPS report. but in flow-document we can add only Block type controls like(Table,Paragraph) and the  chart is a visual type object so how can i add  a chart in flow document.

     public static Chart Loadgraph() {
          Chart chart = new Chart();
          LineSeries series = new LineSeries();
          chart.Series.Add(series);
          Binding key = new Binding { Path = new PropertyPath("Key") };
          Binding value = new Binding { Path = new PropertyPath("Value") };
          series.IndependentValueBinding = key;
          series.DependentValueBinding = value;
          ((LineSeries)chart.Series[0]).ItemsSource =
              new[]{
                new KeyValuePair<int, int>(0, 100),
                new KeyValuePair<int, int>(45, 130),
                new KeyValuePair<int, int>(15, 150),
                new KeyValuePair<int, int>(20, 125),
                new KeyValuePair<int, int>(25, 155) };
          return chart;
        }
    
    flowdocument.blocks.add(chart);//iam getting exception


    • Edited by sajithtgs Wednesday, February 29, 2012 11:39 AM
    Wednesday, February 29, 2012 10:39 AM

Answers

  • Hi sajithtgs,

    Please check out below sample code for your reference:

     public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                Loaded+=new RoutedEventHandler(MainWindow_Loaded);
            }
    
            public void MainWindow_Loaded(Object o, RoutedEventArgs e)
            {
                List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
          valueList.Add(new KeyValuePair<string, int>("Developer",60));
          valueList.Add(new KeyValuePair<string, int>("Misc", 20));
          valueList.Add(new KeyValuePair<string, int>("Tester", 50));
          valueList.Add(new KeyValuePair<string, int>("QA", 30));
          valueList.Add(new KeyValuePair<string, int>("Project Manager", 40));
    
                Chart ChartLine = new Chart();
                ChartLine.Height = 250;
                ChartLine.Title="Line Series Demo";
                LineSeries LS = new LineSeries();
                LS.DependentValuePath = "Value";
                LS.IndependentValuePath = "Key";
                LS.ItemsSource = valueList;
                ChartLine.Series.Add(LS);
                BlockUIContainer c = new BlockUIContainer(ChartLine);
                Flowdocument.Blocks.Add(c);
            }
        }
     <Grid>
            <FlowDocumentPageViewer>
                <FlowDocument Name="Flowdocument">
                   
                </FlowDocument>
            </FlowDocumentPageViewer>
            <StackPanel Name="SK"/>
        </Grid>

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 6, 2012 6:32 AM
    Moderator

All replies

  • I believe the answer to your problem is the BlockUiContainer.

          <FlowDocumentPageViewer>
            <FlowDocument>
                <Paragraph>
                    Don't click the button!
                </Paragraph>
                <BlockUIContainer>
                    <Button Content="Click Me!" />
                </BlockUIContainer>
             </FlowDocument>
          </FlowDocumentPageViewer>
    

    Regards,
    Pete

    #PEJL

    Wednesday, February 29, 2012 11:33 AM
    Moderator
  • Great,tons of thanks 100/100
    Wednesday, February 29, 2012 11:51 AM
  • but there is a problem XAML-Guy ,while loading graph the data in graph are not showing

     only structure is showing.please see the attachment(Series 1)

      BlockUIContainer c = new BlockUIContainer(XPSGraphs.Loadgraph());
          document.Blocks.Add(c);//only structure is showing


    • Edited by sajithtgs Wednesday, February 29, 2012 12:50 PM
    Wednesday, February 29, 2012 12:49 PM
  • Check Visual Studio "Output" tab for binding errors.

    I suspect you are having problem with what is considered the data context for your control within the document.

    Give your Window/Page a name, then refer to it as the ElementName in your binding code.

      

    If still no joy, post a replicatable example as a new ticket and we will help you fix it.

      

    Good luck,
    Pete


    #PEJL

    Wednesday, February 29, 2012 4:18 PM
    Moderator
  • many thanks,Pete

         i was not able to understand wt u wrote here .can u please correct my code if needed.

    or give a sample code here iam creating every thing programically.

    XPSGraph.CS

     

    public static ChartLoadgraph(){
    Chart chart =newChart();
    LineSeries series =newLineSeries();
    chart
    .Series.Add(series);
    Binding key =newBinding{Path=newPropertyPath("Key")};
    Binding value =newBinding{Path=newPropertyPath("Value")};
    series
    .IndependentValueBinding= key;
    series
    .DependentValueBinding= value;
    ((LineSeries)chart.Series[0]).ItemsSource=
    new[]{
    newKeyValuePair<int,int>(0,100),
    newKeyValuePair<int,int>(45,130),
    newKeyValuePair<int,int>(15,150),
    newKeyValuePair<int,int>(20,125),
    newKeyValuePair<int,int>(25,155)};
      BlockUIContainer c = new BlockUIContainer(chart);

          document.Blocks.Add(c);//only structure is showing    


    Wednesday, February 29, 2012 4:51 PM
  • hi,pete

    i uploaded my solution on skydrive can u please run my solution and try to solve my issue.

    any other suggession are welcome.

    https://skydrive.live.com/?cid=96FB4F1E3660C28B&id=96FB4F1E3660C28B!103

    Wednesday, February 29, 2012 5:56 PM
  • Hi sajithtgs,

    Please check out below sample code for your reference:

     public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                Loaded+=new RoutedEventHandler(MainWindow_Loaded);
            }
    
            public void MainWindow_Loaded(Object o, RoutedEventArgs e)
            {
                List<KeyValuePair<string, int>> valueList = new List<KeyValuePair<string, int>>();
          valueList.Add(new KeyValuePair<string, int>("Developer",60));
          valueList.Add(new KeyValuePair<string, int>("Misc", 20));
          valueList.Add(new KeyValuePair<string, int>("Tester", 50));
          valueList.Add(new KeyValuePair<string, int>("QA", 30));
          valueList.Add(new KeyValuePair<string, int>("Project Manager", 40));
    
                Chart ChartLine = new Chart();
                ChartLine.Height = 250;
                ChartLine.Title="Line Series Demo";
                LineSeries LS = new LineSeries();
                LS.DependentValuePath = "Value";
                LS.IndependentValuePath = "Key";
                LS.ItemsSource = valueList;
                ChartLine.Series.Add(LS);
                BlockUIContainer c = new BlockUIContainer(ChartLine);
                Flowdocument.Blocks.Add(c);
            }
        }
     <Grid>
            <FlowDocumentPageViewer>
                <FlowDocument Name="Flowdocument">
                   
                </FlowDocument>
            </FlowDocumentPageViewer>
            <StackPanel Name="SK"/>
        </Grid>

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 6, 2012 6:32 AM
    Moderator
  • Thanks Annabella,

      this works,

    Regards,

    sajith

    Tuesday, March 6, 2012 6:39 AM
  • Thanks for your reply sajith.

    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 6, 2012 6:54 AM
    Moderator
  • Annabella,

    I hope you can help me.  I am trying to achieve a similar solution.  I need to dynamically add Line Series to my chart.  My project is in VB.Net.  I try to execute the following statements:

    Dim mySeries As System.Windows.Controls.DataVisualization.Charting.LineSeries
    mySeries = New System.Windows.Controls.DataVisualization.Charting.LineSeries()
    
    With mySeries
        .Title = myPidNumber.ToString
        .DependentValuePath = "Value"
        .IndependentValuePath = "Key"
        .ItemsSource = procVal
        .IsSelectionEnabled = True
    End With
    

    When I do, I get this error on the second statement above:

    Locating source for 'C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\LineSeries.cs'. Checksum: MD5 {1b c6 9c 8d 23 3b 86 df 36 1f c1 94 a2 a3 79 66}
    The file 'C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\LineSeries.cs' does not exist.
    Looking in script documents for 'C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\LineSeries.cs'...
    Looking in the projects for 'C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\LineSeries.cs'.
    The file was not found in a project.
    Looking in directory 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\crt\src\'...
    Looking in directory 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'...
    Looking in directory 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'...
    Looking in directory 'c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\atlmfc\include\'...
    The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\LineSeries.cs.
    The debugger could not locate the source file 'C:\dd\WPF_1\src\wpf\src\ControlsPack\WPFToolkit\DataVisualization\Charting\Series\LineSeries.cs'.
    

    I can't figure this out.  Am I supposed to already have one series defined in XAML?  Some of the articles say I don't need to.  Why can't I instantiate a LineSeries object?

    Thanks...


    Ron Mittelman

    Wednesday, June 12, 2013 6:04 PM