Skip to main content
RSS feed Subscribe to feed

 

Creating a Line Chart

Code examples explaining how to set up line charts.

Refer to What is a Line Chart? for a line chart presentation.

Create and Configure a Line Chart

public static void CreateLineChart(AnalysisApplication application)
{
    // Add a line chart to the page
    LineChart lineChart = application.Document.ActivePageReference.Visuals.AddNew<LineChart>();
    lineChart.Title = "Basic Line Chart";

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

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

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

    // Set the x-axis
    lineChart.XAxis.Expression = "Month";

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

Creating a Line Chart with Individual Scaling

public static void CreateLineChartWithIndividualScaling(AnalysisApplication application)
{
    // Add a line chart to the page
    LineChart lineChart = application.Document.ActivePageReference.Visuals.AddNew<LineChart>();
    lineChart.Title = "Line Chart";

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

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

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

    // Set the x-axis
    lineChart.XAxis.Expression = "Month";

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

    // Set one line for each measure on the y-axis
    lineChart.ColorAxis.Expression = "<[Axis.Default.Names]>";

    // Scale each line independently
    lineChart.YAxis.IndividualScaling = true;
    lineChart.YAxis.IndividualScalingMode = IndividualScalingMode.Color;

    // Set the scale for sales on the right and cost on the left hand side
    lineChart.YAxis.Scale.IndexedDock["Sum(Cost)"] = ScaleDock.Near;
    lineChart.YAxis.Scale.IndexedDock["Sum(Sales)"] = ScaleDock.Far;
}

Creating a Line Chart with Formatting

public static void CreateLineChartWithFormatting(AnalysisApplication application)
{
    // Add a line chart to the page
    LineChart lineChart = application.Document.ActivePageReference.Visuals.AddNew<LineChart>();
    lineChart.Title = "Line Chart";

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

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

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

    // Set the x-axis
    lineChart.XAxis.Expression = "Month";

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

    // Set one line for each measure on the y-axis
    lineChart.ColorAxis.Expression = "<[Axis.Default.Names]>";

    // Scale each line independently
    lineChart.YAxis.IndividualScaling = true;
    lineChart.YAxis.IndividualScalingMode = IndividualScalingMode.Color;

    // Set the scale for sales on the right and cost on the left hand side
    lineChart.YAxis.Scale.IndexedDock["Sum(Cost)"] = ScaleDock.Near;
    lineChart.YAxis.Scale.IndexedDock["Sum(Sales)"] = ScaleDock.Far;

    // Create a formatter for the left scale
    NumberFormatter leftFormatter = (NumberFormatter)DataType.Real.CreateLocalizedFormatter();
    NumberFormatter rightFormatter = (NumberFormatter)DataType.Real.CreateLocalizedFormatter();

    // Set the category to Number
    leftFormatter.Category = NumberFormatCategory.Number;
    rightFormatter.Category = NumberFormatCategory.Number;

    // Set the number of decimals to 2 on the left and 0 on the right
    leftFormatter.DecimalDigits = 2;
    rightFormatter.DecimalDigits = 0;

    // Set formatting on the y-axis
    lineChart.YAxis.Scale.Formatting.IndexedRealFormatter["Sum(Cost)"] = leftFormatter;
    lineChart.YAxis.Scale.Formatting.IndexedRealFormatter["Sum(Sales)"] = rightFormatter;
}

Creating a Line Chart with Short Number Formatting

public static void CreateLineChartWithShortFormatting(AnalysisApplication application)
{
    // Add a line chart to the page
    LineChart lineChart = application.Document.ActivePageReference.Visuals.AddNew<LineChart>();
    lineChart.Title = "Line Chart";

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

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

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

    // Set the x-axis
    lineChart.XAxis.Expression = "Month";

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

    // Set one line for each measure on the y-axis
    lineChart.ColorAxis.Expression = "<[Axis.Default.Names]>";

    // Scale each line independently
    lineChart.YAxis.IndividualScaling = true;
    lineChart.YAxis.IndividualScalingMode = IndividualScalingMode.Color;

    // Set the scale for sales on the right and cost on the left hand side
    lineChart.YAxis.Scale.IndexedDock["Sum(Cost)"] = ScaleDock.Near;
    lineChart.YAxis.Scale.IndexedDock["Sum(Sales)"] = ScaleDock.Far;

    // Create a formatter for the left scale
    NumberFormatter leftFormatter = (NumberFormatter)DataType.Real.CreateLocalizedFormatter();
    NumberFormatter rightFormatter = (NumberFormatter)DataType.Real.CreateLocalizedFormatter();

    // Set the category to Number
    leftFormatter.Category = NumberFormatCategory.Number;
    rightFormatter.Category = NumberFormatCategory.Currency;

    // Create a short formatting symbol scheme
    List<ShortFormattingSymbol> symbols = new List<ShortFormattingSymbol>(3);
    symbols.Add(new ShortFormattingSymbol(" ten", 1));
    symbols.Add(new ShortFormattingSymbol(" hundred", 2));
    symbols.Add(new ShortFormattingSymbol(" thousand", 3));

    ShortFormattingSymbolScheme scheme = new ShortFormattingSymbolScheme("Simple", symbols);

    // Set the number of decimals to 2 on the left formatter.
    leftFormatter.DecimalDigits = 2;

    // Use $ as currency symbol on the right formatter.
    rightFormatter.DecimalDigits = 2;
    rightFormatter.CurrencyCulture = new System.Globalization.CultureInfo("en-US");

    // Set short number formatting and auto decimals.
    rightFormatter.ShortFormattingEnabled = true;
    rightFormatter.ShortFormattingSymbolScheme = scheme;
    rightFormatter.DecimalDigitsMode = DecimalDigitsMode.Auto;

    // Set formatting on the left and right Y-axis scales.
    lineChart.YAxis.Scale.Formatting.IndexedRealFormatter["Sum(Cost)"] = leftFormatter;
    lineChart.YAxis.Scale.Formatting.IndexedRealFormatter["Sum(Sales)"] = rightFormatter;
}

Creating a Line Chart with Error Bars

public static void CreateLineChartWithErrorBars(AnalysisApplication application)
{
    // Add a line chart to the page
    LineChart lineChart = application.Document.ActivePageReference.Visuals.AddNew<LineChart>();
    lineChart.Title = "Line Chart with Error Bars";

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

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

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

    // Set the x-axis
    lineChart.XAxis.Expression = "Month";

    // Set the y-axis
    lineChart.YAxis.Expression = "Sum([Sales]) AS Sales, Sum([Cost]) AS Cost";

    // Set one line for each measure on the y-axis
    lineChart.ColorAxis.Expression = "<[Axis.Default.Names]>";

    // Add an error to the "Sales" line
    CategoryKey salesKey = new CategoryKey("Sales");
    lineChart.YAxis.ErrorBars.IndexedEnabled[salesKey] = true;
    lineChart.YAxis.ErrorBars.IndexedUpperExpression[salesKey] = "StdErr([Sales])";
    lineChart.YAxis.ErrorBars.IndexedLowerExpression[salesKey] = "StdErr([Sales])";

    // Add an error to the "Cost" line
    CategoryKey costKey = new CategoryKey("Cost");
    lineChart.YAxis.ErrorBars.IndexedEnabled[costKey] = true;
    lineChart.YAxis.ErrorBars.IndexedUpperExpression[costKey] = "StdErr([Cost])";
    lineChart.YAxis.ErrorBars.IndexedLowerExpression[costKey] = "StdErr([Cost])";

    // Remove the end caps from the error bars
    lineChart.YAxis.ErrorBars.ShowEndCaps = false;
}