Stacked charts

Now, let's learn how to create a Stacked Chart. We will create a chart showcasing revenue split for each quarter of the current year by product category - food products and non-food products. The data will look as shown below:

Quarter Product Type Revenue(in USD)
Q1 Food Products 11000
Non-Food Products 11400
Q2 Food Products 15000
Non-Food Products 14800
Q3 Food Products 13500
Non-Food Products 8300
Q4 Food Products 15000
Non-Food Products 11800

The chart will look as shown below:

FusionCharts will load here..

Click here to edit the stacked chart.

Render the Chart

Create the StackedChart.aspx.cs file and do the following:

  • Include the FusionCharts.DataEngine and FusionCharts.Visualization .dll files.
  • Create DataTable.
  • Retrieve data using database query.
  • Set server name.
  • Set DataBase name.
  • Connect with DataBase using a connection string.
  • Create StaticSource using the data table.
  • Create an instance of DataModel class.
  • Add DataSource to the DataModel.
  • Instantiate Stacked Chart.
  • Set chart width.
  • Set chart height.
  • Set the type of stacked chart as BAR, COLUMN, or AREA.
  • Set DataModel instance as the data source of the chart.
  • Set Chart title.
  • Finally, use a container using <div> to render the chart.

The code is shown below:

using FusionCharts.DataEngine;
using FusionCharts.Visualization;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FusionChartsVisualisationWebFormsSamples.Samples {
    public partial class StackedChart : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            DataTable ChartData = new DataTable();
            string query = "select * from ProductRevenue";
            string connetionstring = null;
            string serverName = "FusionChartsServer";
            string databaseName = "FusionchartsSamplesDB";
            ChartData.Clear();
            connetionstring = "Data Source=" + serverName + ";Initial Catalog=" + databaseName + ";Trusted_Connection=true;";
            using (SqlConnection con = new SqlConnection(connetionstring))
            {
                con.Open();
                using (SqlCommand command = new SqlCommand(query, con))
                using (SqlDataAdapter da = new SqlDataAdapter(command))
                {
                    da.Fill(ChartData);
                }
            }
            StaticSource source = new StaticSource(ChartData);
            DataModel model = new DataModel();
            model.DataSources.Add(source);
            Charts.StackedChart stack = new Charts.StackedChart("stacked_chart_db");
            stack.Width = "700";
            stack.Height = "400";
            stack.Caption.Text = "Revenue split by product aategory";
            stack.Caption.Bold = true;
            stack.SubCaption.Text = "for current year";
            stack.XAxis.Text = "Quarter";
            stack.YAxis.Text = "Revenue";
            stack.Data.Source = model;
            Literal1.Text = stack.Render();
        }
    }
}

The .aspx template for the above sample is shown below:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="StackedChart.aspx.cs" Inherits="FusionChartsVisualisationWebFormsSamples.Samples.StackedChart" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <script type="text/javascript" src="//cdn.fusioncharts.com/fusioncharts/latest/fusioncharts.js"></script>
    <script type="text/javascript" src="//cdn.fusioncharts.com/fusioncharts/latest/themes/fusioncharts.theme.fusion.js"></script>
    <form id="form1" runat="server">
        <div>
            <asp:Literal ID ="Literal1" runat ="server"></asp:Literal>
        </div>
        <div>
            <input type ="button" value ="Samples" onclick="location.href = 'Index.aspx';" />
        </div>
    </form>
</body>
</html>
Was this article helpful to you ?