Set Spotfire Graph Axis Range Programatically

11,225

Solution 1

I found an automated solution, and you can either set it to use zoom sliders or not. You have to input the StartDate and EndDate properties as Script parameters, and you also have to enter every visualization as a Script parameter (so I needed 5 total for my script). The code is posted below - hope this helps someone!

NO ZOOM SLIDERS:

#Import the necessary namespaces
from Spotfire.Dxp.Application.Visuals import VisualContent
from Spotfire.Dxp.Application.Visuals import AxisRange
from Spotfire.Dxp.Data import DataPropertyClass

#Define variables
min = Document.Properties["StartDate"]
max = Document.Properties["EndDate"]

#Cast visual as specific plot type
#Note use of VisualContent for any chart type
plot1= plot1.As[VisualContent]()
plot2= plot2.As[VisualContent]()
plot3= plot3.As[VisualContent]()

#Set visualization parameters
plot1.XAxis.Range = AxisRange(min,max);
plot2.XAxis.Range = AxisRange(min,max);
plot3.XAxis.Range = AxisRange(min,max);

WITH ZOOM SLIDERS:

#Import the necessary namespaces
from Spotfire.Dxp.Application.Visuals import VisualContent
from Spotfire.Dxp.Application.Visuals import AxisRange
from Spotfire.Dxp.Data import DataPropertyClass

#Define variables
min = Document.Properties["StartDate"]
max = Document.Properties["EndDate"]

#Cast visual as specific plot type
#Note use of VisualContent for any chart type
plot1= plot1.As[VisualContent]()
plot2= plot2.As[VisualContent]()
plot3= plot3.As[VisualContent]()

#Set visualization parameters
plot1.XAxis.ManualZoom = True
plot2.XAxis.ManualZoom = True
plot3.XAxis.ManualZoom = True
plot1.XAxis.ZoomRange = AxisRange(min,max);
plot2.XAxis.ZoomRange = AxisRange(min,max);
plot3.XAxis.ZoomRange = AxisRange(min,max);

Solution 2

I think you can achieve this by filtering on a Boolean calculated column instead of depending on IronPython scripting.

To work through this I created a dummy data set of product sales by region. I made a line chart with Sum(Sales) on the Y-axis and Sales Date on the X-axis.

enter image description here

Next I added a new Text Area and added two input field Property Controls. I named the Document Properties dateBegin and dateEnd, each of data type Date.

Property Control Setup

Now I can enter my desired date range.

Date Range Entry Fields

Now I need to make my X-axis (Sales Date) only reflect the date range that the user entered. I chose to take advantage of Spotfire's filtering capabilities. I inserted a Boolean Calculated Column which I named RowInDateRange.

Edit Calculated Column

In case it's too hard to read in the image, here's the custom expression:

CASE WHEN [Sales Date] >= Date("${dateBegin}") AND [Sales Date] <= Date("${dateEnd}") THEN True ELSE False END

So now I have a column (RowInDateRange) that tells me whether a given row is within the user's entered date range, and I can easily filter out the rows that aren't.

Filter out rows not in date range

The visualization now dynamically reflects the user's entered date range. I have saved a DXP sample that you are welcome to check out: Six Months Sales by Region.dxp

I hope this was helpful.

Share:
11,225
greenbellpepper
Author by

greenbellpepper

Updated on June 14, 2022

Comments

  • greenbellpepper
    greenbellpepper about 2 years

    I am trying to program a simple IronPython script to set the date range of my line graph x-axis in Spotfire based on property control user inputs. I cannot find any documentation or examples of this, so here's what I guessed, but it's not working:

    from Spotfire.Dxp.Application.Visuals import LineChart
    from Spotfire.Dxp.Application.Visuals import ScaleAxis
    
    plot1= plot1.As[LineChart]()
    plot1.XAxis.Min = StartDate
    plot1.XAxis.Max = EndDate
    

    I entered the visualization and the property control as Script Parameters in the Edit Script window. Does anyone know how to make this work?? Thanks!

    Note: I do not want to use zoom sliders.


    Update: I am doing a Sum([Column]) OVER (AllPrevious([Date])) so I can't filter any dates out for the calculation of Y. I just need them filtered out of the X axis range. The original table is structured like this: | Product | Date | Phase |

    And I have calculated columns for each of the phases with formulas: If([Phase]="Phase A",1,0)

    Essentially the graph shows how many products have gone through each stage of production over time, so I need to always know the completion date of each phase to count the number of products in that phase on any particular date. I only want to see a certain date range on the X-axis though because I have some other graphs placed above/below from 2 other tables to compare sales and costs, and it's easier to analyze based on the number of products in each phase when the axes show the same dates. Sorry I can't upload pictures so I hope that makes sense.

    We only care about the most recent sales/costs, and I restrict those dates with the filters using Data on Demand for the sales and costs tables so the graphs automatically show what I want. Now the only issue is filtering the range for the # of Products graph because the dates in the phase columns can go back years, and we need to know how many total products are in each phase, but we don't want to see the graph from the beginning of time...I can achieve this with a zoom slider or hardcoding the min/max in that graph, but I was really hoping for something automated so we don't have to continually adjust it.

  • greenbellpepper
    greenbellpepper over 9 years
    Thanks for the quick response. This is a good solution for most graphs. I added more information on my case in the original question because unfortunately the filter approach will affect the values I need to show in the graph. Sorry I didn't provide more up front on why I was going the IronPython route - I am definitely open to other options though as long as I can get the final result with the total number of products!