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";
            connetionstring = "Data Source=" + serverName + ";Initial Catalog=" + databaseName + ";Trusted_Connection=true;";
            using (SqlConnection con = new SqlConnection(connetionstring))
                using (SqlCommand command = new SqlCommand(query, con))
                using (SqlDataAdapter da = new SqlDataAdapter(command))
            StaticSource source = new StaticSource(ChartData);
            DataModel model = new DataModel();
            Charts.StackedChart stack = new Charts.StackedChart("stacked_chart_db");
            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="">
  <head runat="server">
    <form id="form1" runat="server">
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
          onclick="location.href = 'Index.aspx';"