Skip to main content
RSS feed Subscribe to feed

 

Creating a Bar Chart

Code example explaining how to set up bar charts.

Refer to What is a Bar Chart? for a bar chart presentation.

Create and Configure a Bar Chart

public static void CreateBarchart(AnalysisApplication application)
{
    // Add a bar chart to the page
    BarChart barChart = application.Document.ActivePageReference.Visuals.AddNew<BarChart>();
    barChart.Title = "Basic Bar Chart";

    // Connect the bar chart to data
    DataManager dataManager = application.Document.Data;
    barChart.Data.DataTableReference = dataManager.Tables.DefaultTableReference;

    // Set filtering for the visualization
    barChart.Data.UseActiveFiltering = true;

    // Set marking for the visualization
    barChart.Data.MarkingReference = dataManager.Markings.DefaultMarkingReference;

    // Set the x-axis
    barChart.XAxis.Expression = "<Type>";

    // Set the y-axis
    barChart.YAxis.Expression = "Sum(Sales)";
}

Create and Configure a Bar Chart with Formatting on the Y Axis

public static void CreateBarchartWithFormatting(AnalysisApplication application)
{
    // Add a bar chart to the page
    BarChart barChart = application.Document.ActivePageReference.Visuals.AddNew<BarChart>();
    barChart.Title = "Bar Chart with Formatting";

    // Connect the bar chart to data
    DataManager dataManager = application.Document.Data;
    barChart.Data.DataTableReference = dataManager.Tables.DefaultTableReference;

    // Set filtering for the visualization
    barChart.Data.UseActiveFiltering = true;

    // Set marking for the visualization
    barChart.Data.MarkingReference = dataManager.Markings.DefaultMarkingReference;

    // Set the x-axis
    barChart.XAxis.Expression = "<Type>";

    // Set the y-axis
    barChart.YAxis.Expression = "Sum(Sales)";

    // Create a number formatter
    NumberFormatter formatter = (NumberFormatter)DataType.Real.CreateLocalizedFormatter();

    // Set the category to currency
    formatter.Category = NumberFormatCategory.Currency;

    // Set the number of decimals to 2 and use $ as currency symbol.
    formatter.DecimalDigits = 2;
    formatter.CurrencyCulture = new System.Globalization.CultureInfo("en-US");

    // Set formatting on the y-axis
    barChart.YAxis.Scale.Formatting.RealFormatter = formatter;

}