Skip to main content

Creating a Scatter Plot

Code examples explaining how to set up scatter plots.

Refer to What is a Scatter Plot? for a scatter plot presentation.

Create and Configure a Scatter Plot

public static void CreateScatterplot(AnalysisApplication application)
{
    // Add a scatter plot to the page
    ScatterPlot scatterplot = application.Document.ActivePageReference.Visuals.AddNew<ScatterPlot>();
    scatterplot.Title = "Basic Scatterplot";

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

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

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

    // Set marker shape
    scatterplot.ShapeAxis.DefaultShape = new MarkerShape(MarkerType.Circle);

    // Marker By
    scatterplot.MarkerByAxis.Expression = "<baserowid()>"; // one marker per row in the dataset

    // X Axis
    scatterplot.XAxis.Expression = "Cost";

    // Y Axis
    scatterplot.YAxis.Expression = "Sales";

    // Color By
    scatterplot.ColorAxis.Expression = "<Type>";

    // Trellis
    scatterplot.Trellis.TrellisMode = TrellisMode.Panels;
    scatterplot.Trellis.PanelAxis.Expression = "<Category>";
}

Creating a Scatter Plot : Aggregated

public static void CreateAggregatedScatterplot(AnalysisApplication application)
{
    // Add a scatter plot to the page
    ScatterPlot scatterplot = application.Document.ActivePageReference.Visuals.AddNew<ScatterPlot>();
    scatterplot.Title = "Aggregated Scatterplot";

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

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

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

    // Marker By
    scatterplot.MarkerByAxis.Expression = "<Type>"; // one marker per type in the dataset

    // X Axis
    scatterplot.XAxis.Expression = "Avg(Cost)";

    // Y Axis
    scatterplot.YAxis.Expression = "Avg(Sales)";

    // Size By
    scatterplot.SizeAxis.Expression = "Sum(Sales)";

    // Color By
    scatterplot.ColorAxis.Expression = "Avg(Cost)";

    // Set marker shape
    scatterplot.ShapeAxis.DefaultShape = new MarkerShape(MarkerType.Circle);
}

Creating a Scatter Plot with Curve Fit

public static void CreateScatterplotWithCurveFit(AnalysisApplication application)
{
    // Add a scatter plot to the page
    ScatterPlot scatterplot = application.Document.ActivePageReference.Visuals.AddNew<ScatterPlot>();
    scatterplot.Title = "Basic Scatterplot";

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

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

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

    // Marker By
    scatterplot.MarkerByAxis.Expression = "<baserowid()>"; // one marker per row in the dataset

    // X Axis
    scatterplot.XAxis.Expression = "Cost";

    // Y Axis
    scatterplot.YAxis.Expression = "Sales";

    // Color By
    scatterplot.ColorAxis.Expression = "<Type>";

    // Add a straight line fit 
    StraightLineFittingModel straightLineFit = scatterplot.FittingModels.AddNew<StraightLineFittingModel>();

    // Fit each color separately
    straightLineFit.IndividualFittingModes = IndividualFittingModes.Color;

    // Change the line style for the resulting straight line
    straightLineFit.Line.LineStyle = LineStyle.Dash;
}

Creating a Scatter Plot with Individual Scaling

public static void CreateScatterPlotWithIndividualScaling(AnalysisApplication application)
{
    // Add a scatterplot to the page
    ScatterPlot scatterplot = application.Document.ActivePageReference.Visuals.AddNew<ScatterPlot>();
    scatterplot.Title = "Scatterplot with Individual Scaling";

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

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

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

    // Set marker shape
    scatterplot.ShapeAxis.DefaultShape = new MarkerShape(MarkerType.Circle);

    // Marker By
    scatterplot.MarkerByAxis.Expression = "<baserowid()>"; // one marker per row in the dataset

    // X Axis
    scatterplot.XAxis.Expression = "Cost";

    // Y Axis
    scatterplot.YAxis.Expression = "Sales";

    // Trellis
    scatterplot.Trellis.TrellisMode = TrellisMode.Panels;
    scatterplot.Trellis.PanelAxis.Expression = "<Type>";

    // Individual Scaling
    scatterplot.YAxis.IndividualScaling = true;
    scatterplot.YAxis.IndividualScalingMode = IndividualScalingMode.Trellis;    
}