Combine Stacked Line Chart and Stacked Bar Chart

2 posts, 0 answers
  1. Luis
    Luis avatar
    4 posts
    Member since:
    Mar 2011

    Posted 03 Jun 2011 Link to this post

    I need to combine a stacked line chart with a stacked bar chart.  So far, I have been successful in combining the stacked line chart with a bar chart or in creating the stacked bar chart but I cannot combine it with the stacked line chart.

    This is the code I'm using,

    Any suggestions?

    Thanks in advance

     

    <BR>
    <P>Dim TheChart As Telerik.Windows.Controls.RadChart = TryCast(sender, 
    Telerik.Windows.Controls.RadChart)<BR>        
    'TheChart.ItemsSource = CreateChart_DefaultView().ItemsSource</P>
    <P>        
    TheChart.DefaultView.ChartTitle.Content = "Year 
    2009"<BR>        
    TheChart.DefaultView.ChartTitle.HorizontalAlignment = 
    HorizontalAlignment.Center<BR>        'Chart 
    Legend<BR>        
    TheChart.DefaultView.ChartLegend.UseAutoGeneratedItems = 
    True<BR>        'Charts 
    values<BR>        Dim LineChartValues() As 
    Integer = {154, 138, 143, 120, 135, 125, 179, 170, 198, 187, 193, 
    176}<BR>        Dim LineChartCats() As String 
    = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", 
    "Dec"}<BR>        Dim ConstantValue As 
    Integer = 140<BR>        'Line 
    Chart<BR>        Dim lineSeries As New 
    DataSeries()<BR>        
    lineSeries.LegendLabel = 
    "Turnover"<BR>        lineSeries.Definition = 
    New LineSeriesDefinition()<BR>        Dim 
    dataPoint As New DataPoint()<BR>        For i 
    As Integer = 0 To LineChartValues.Length - 
    1<BR>            
    dataPoint = New 
    DataPoint()<BR>            
    dataPoint.YValue = 
    LineChartValues(i)<BR>            
    dataPoint.XCategory = 
    LineChartCats(i)<BR>            
    lineSeries.Add(dataPoint)<BR>        
    Next<BR>        
    TheChart.DefaultView.ChartArea.DataSeries.Add(lineSeries)</P>
    <P>        ' Line chart 
    (constant)<BR>        Dim lineSeriesB As New 
    DataSeries()<BR>        
    lineSeriesB.LegendLabel = 
    "Turnover"<BR>        lineSeriesB.Definition 
    = New LineSeriesDefinition()<BR>        For i 
    As Integer = 0 To LineChartValues.Length - 
    1<BR>            
    dataPoint = New 
    DataPoint()<BR>            
    dataPoint.YValue = 
    ConstantValue<BR>            
    dataPoint.XCategory = 
    LineChartCats(i)<BR>            
    lineSeriesB.Add(dataPoint)<BR>        
    Next<BR>        
    TheChart.DefaultView.ChartArea.DataSeries.Add(lineSeriesB)</P>
    <P>        'Bar 
    Chart<BR>        Dim BarChartValues() As 
    Integer = {5, 18, 12, 11, 15, 20, 10, 7, 15, 25, 10, 
    5}<BR>        Dim BarChartValuesB() As 
    Integer = {15, 25, 13, 8, 5, 10, 15, 20, 10, 13, 15, 
    10}<BR>        Dim BarChartValuesC() As 
    Integer = {10, 20, 15, 10, 5, 14, 9, 11, 20, 16, 10, 
    15}<BR>        Dim BarChartCats() As String = 
    {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", 
    "Dec"}<BR>        Dim barSeries As New 
    DataSeries()<BR>        Dim sampleData As New 
    List(Of Company)()<BR>        
    barSeries.LegendLabel = "Expenses"<BR>        
    barSeries.Definition = New 
    BarSeriesDefinition()<BR>        For j As 
    Integer = 0 To BarChartValues.Length - 
    1<BR>            
    sampleData.Add(New Company("Name " & j, BarChartValues(j), 
    BarChartValuesB(j), 
    BarChartValuesC(j)))<BR>        Next</P>
    <P>        Dim SeriesMappingList As New 
    List(Of SeriesMapping)<BR>        Dim 
    seriesMapping As New SeriesMapping()</P>
    <P>        seriesMapping = New 
    SeriesMapping()<BR>        
    seriesMapping.LegendLabel = "Label 
    1"<BR>        
    seriesMapping.ItemMappings.Add(New ItemMapping("Value1", 
    DataPointMember.YValue))<BR>        
    seriesMapping.ItemMappings.Add(New ItemMapping("Name", 
    DataPointMember.XCategory))<BR>        
    seriesMapping.SeriesDefinition = New 
    StackedBarSeriesDefinition()<BR>        
    seriesMapping.SeriesDefinition.ShowItemLabels = 
    True<BR>        
    SeriesMappingList.Add(seriesMapping)<BR>        
    seriesMapping = New 
    SeriesMapping()<BR>        
    seriesMapping.LegendLabel = "Label 
    2"<BR>        
    seriesMapping.ItemMappings.Add(New ItemMapping("Value2", 
    DataPointMember.YValue))<BR>        
    seriesMapping.ItemMappings.Add(New ItemMapping("Name", 
    DataPointMember.XCategory))<BR>        
    seriesMapping.SeriesDefinition = New 
    StackedBarSeriesDefinition()<BR>        
    seriesMapping.SeriesDefinition.ShowItemLabels = 
    True<BR>        
    SeriesMappingList.Add(seriesMapping)<BR>        
    seriesMapping = New 
    SeriesMapping()<BR>        
    seriesMapping.LegendLabel = "Laabel 
    3"<BR>        
    seriesMapping.ItemMappings.Add(New ItemMapping("Value3", 
    DataPointMember.YValue))<BR>        
    seriesMapping.ItemMappings.Add(New ItemMapping("Name", 
    DataPointMember.XCategory))<BR>        
    seriesMapping.SeriesDefinition = New 
    StackedBarSeriesDefinition()<BR>        
    seriesMapping.SeriesDefinition.ShowItemLabels = 
    True<BR>        
    SeriesMappingList.Add(seriesMapping)</P>
    <P>        'seriesMapping = New 
    SeriesMapping()<BR>        
    'seriesMapping.SeriesDefinition = New 
    LineSeriesDefinition()<BR>        
    'seriesMapping.ItemMappings.Add(New ItemMapping("Line1", 
    DataPointMember.YValue))<BR>        
    'seriesMapping.ItemMappings.Add(New ItemMapping("StageVariable", 
    DataPointMember.XCategory))<BR>        
    'seriesMapping.SeriesDefinition.LegendDisplayMode = 
    LegendDisplayMode.DataPointLabel<BR>        
    'SeriesMappingList.Add(seriesMapping)<BR>        
    'seriesMapping = New 
    SeriesMapping()<BR>        
    'seriesMapping.SeriesDefinition = New 
    LineSeriesDefinition()<BR>        
    'seriesMapping.ItemMappings.Add(New ItemMapping("Line2", 
    DataPointMember.YValue))<BR>        
    'seriesMapping.ItemMappings.Add(New ItemMapping("StageConstant", 
    DataPointMember.XCategory))<BR>        
    'seriesMapping.SeriesDefinition.LegendDisplayMode = 
    LegendDisplayMode.DataPointLabel<BR>        
    'SeriesMappingList.Add(seriesMapping)</P>
    <P>        TheChart.ItemsSource = 
    sampleData</P>
    <P>        
    TheChart.DefaultView.ChartArea.AxisY.AutoRange = 
    True<BR>        
    'TheChart.DefaultView.ChartArea.AxisY.AddRange(0, 200, 
    100)<BR>        
    TheChart.SeriesMappings.Clear()<BR>        
    For n = 0 To SeriesMappingList.Count - 
    1<BR>            
    TheChart.SeriesMappings.Add(SeriesMappingList(n))<BR>        
    Next</P>
  2. Tsvetie
    Admin
    Tsvetie avatar
    1517 posts

    Posted 09 Jun 2011 Link to this post

    Hi Luis,
    The code that you have posted is correct. However, you have not taken into account that when the chart is data bound, it clears the DataPoints in its series and as a result, only the series that show data from the ItemsSource are drawn. In case you need a couple of series that are data-bound and a couple of series, for which you create the DataPoints manually, please move the code that creates the latter to the DataBound handler of the chart. For example:
    Private Sub MainWindow_Initialized(sender As Object, e As System.EventArgs) Handles Me.Initialized
        Dim BarChartValues() As Integer = {5, 18, 12, 11, 15, 20, 10, 7, 15, 25, 10, 5}
        Dim BarChartValuesB() As Integer = {15, 25, 13, 8, 5, 10, 15, 20, 10, 13, 15, 10}
        Dim BarChartValuesC() As Integer = {10, 20, 15, 10, 5, 14, 9, 11, 20, 16, 10, 15}
        Dim BarChartCats() As String = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
     
        Dim sampleData As New List(Of Company)()
        For j As Integer = 0 To BarChartValues.Length - 1
            sampleData.Add(New Company(BarChartCats(j), BarChartValues(j), BarChartValuesB(j), BarChartValuesC(j)))
        Next
     
        Dim seriesMapping As New SeriesMapping()
        seriesMapping.LegendLabel = "Label 1"
        seriesMapping.SeriesDefinition = New StackedBarSeriesDefinition()
        seriesMapping.ItemMappings.Add(New ItemMapping("Value1", DataPointMember.YValue))
        seriesMapping.ItemMappings.Add(New ItemMapping("Name", DataPointMember.XCategory))
        seriesMapping.SeriesDefinition.ShowItemLabels = True
        TheChart.SeriesMappings.Add(seriesMapping)
     
        seriesMapping = New SeriesMapping()
        seriesMapping.LegendLabel = "Label 2"
        seriesMapping.ItemMappings.Add(New ItemMapping("Value2", DataPointMember.YValue))
        seriesMapping.ItemMappings.Add(New ItemMapping("Name", DataPointMember.XCategory))
        seriesMapping.SeriesDefinition = New StackedBarSeriesDefinition()
        seriesMapping.SeriesDefinition.ShowItemLabels = True
        TheChart.SeriesMappings.Add(seriesMapping)
     
        seriesMapping = New SeriesMapping()
        seriesMapping.LegendLabel = "Laabel 3"
        seriesMapping.ItemMappings.Add(New ItemMapping("Value3", DataPointMember.YValue))
        seriesMapping.ItemMappings.Add(New ItemMapping("Name", DataPointMember.XCategory))
        seriesMapping.SeriesDefinition = New StackedBarSeriesDefinition()
        seriesMapping.SeriesDefinition.ShowItemLabels = True
        TheChart.SeriesMappings.Add(seriesMapping)
     
        TheChart.ItemsSource = sampleData
    End Sub
     
    Private Sub TheChart_DataBound(sender As Object, e As Telerik.Windows.Controls.Charting.ChartDataBoundEventArgs) Handles TheChart.DataBound
        Dim LineChartValues() As Integer = {154, 138, 143, 120, 135, 125, 179, 170, 198, 187, 193, 176}
        Dim LineChartCats() As String = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
        Dim ConstantValue As Integer = 140
     
        Dim lineSeries As New DataSeries()
        lineSeries.LegendLabel = "Turnover"
        lineSeries.Definition = New StackedLineSeriesDefinition()
     
        Dim dataPoint As New DataPoint()
        For i As Integer = 0 To LineChartValues.Length - 1
            dataPoint = New DataPoint()
            dataPoint.YValue = LineChartValues(i)
            dataPoint.XCategory = LineChartCats(i)
            lineSeries.Add(dataPoint)
        Next
     
        TheChart.DefaultView.ChartArea.DataSeries.Add(lineSeries)
     
        Dim lineSeriesB As New DataSeries()
        lineSeriesB.LegendLabel = "Turnover"
        lineSeriesB.Definition = New StackedLineSeriesDefinition()
     
        For i As Integer = 0 To LineChartValues.Length - 1
            dataPoint = New DataPoint()
            dataPoint.YValue = ConstantValue
            dataPoint.XCategory = LineChartCats(i)
            lineSeriesB.Add(dataPoint)
        Next
     
        TheChart.DefaultView.ChartArea.DataSeries.Add(lineSeriesB)
    End Sub

    In case you need additional help, please open a formal support ticket and send me a running test project that demonstrates the concrete problem that you have, together with detailed information on the expected result.

    All the best,
    Tsvetie
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top