4 Replies Latest reply on Mar 23, 2018 8:19 PM by Maryam Honari

    Define the start and end of a color spectrum legend with code

    Maryam Honari

      Hi,

       

      I have a heat map of London areas. which I change the background with a drop-down category selection.

      Screen Shot 2018-03-13 at 1.44.00 PM.png

       

      I change the values of spectrum legend with the selection of the category with the following code.

      Screen Shot 2018-03-13 at 1.44.26 PM.png

       

      So now the value is defined based on the choice of category. There is a way to select the start and the end of the legend manually, but I want to define this with code based on category.

      Screen Shot 2018-03-13 at 1.36.11 PM.png

       

      I want to define the start and the end fields of the above panel to be defined by my code based on the choice of category. So my main question here will be ":how to define the start and end fields from code?"

        • 1. Re: Define the start and end of a color spectrum legend with code
          swaroop.gantela

          Maryam,

           

          If I am understanding correctly, you are seeking to set the start and end points of colors

          differently for each metric.

           

          Tableau can automatically set it to the max and min based on that metric, but if you seek to

          hard code those points, you could try a calculated field like:

           

          IF [Select Metric]="Sales" AND [Selected Metric]< 10000  THEN 10000

          ELSEIF [Select Metric]="Sales" AND [Selected Metric]> 300000 THEN 300000

          ELSEIF [Select Metric]="Profit" AND [Selected Metric]< -20 THEN -20

          ELSEIF [Select Metric]="Profit" AND [Selected Metric]> 150 THEN 150

          ELSEIF [Select Metric]="Discount" AND [Selected Metric]< .1 THEN .1

          ELSEIF [Select Metric]="Discount" AND [Selected Metric]> .6 THEN .6

          ELSE [Selected Metric]

          END

           

          This should fix the endpoints of the color scale according to the metric.

          Please see the workbook attached in the Forum Thread.

          • 2. Re: Define the start and end of a color spectrum legend with code
            Maryam Honari

            Thanks for your answer,

            But this code doesn't solve my problem. it maps the values of the selected metric to new values, but I don't want to change my values.

            In the quantitative colour palette, if we do not set the end and start of the range, tableau will automatically set it to max and min of existing values( ie. in your dataset minimum profit is -36.2 and max is 204).

            if I want to set a wider range for the color palette (ie. -100 to 500) I can set it with start and end in the following panel. But I want to set those from code without changing the values of metrics like profit?

            Screen Shot 2018-03-16 at 2.06.30 PM.png

            • 3. Re: Define the start and end of a color spectrum legend with code
              swaroop.gantela

              Maryam,

               

              My apologies for misunderstanding.

               

              I couldn't think of way to do that completely inside Tableau,

              but there may be a way with some kind of data padding.

               

              I'm likely wrong, but my feeling is that in order to code the start and end points,

              the value will have to be somewhere in the data.

               

              This may not at all be feasible for you, but one way to force external values

              would be to put the highs and lows in a separate table and union that to your true data.

              This may not be feasible for your datasource type or setup.

               

              In the attempt attached in the Forum Thread, the data was in Excel in the form:

              State | Metric1 | Metric2

              Texas | 5 | -5

              Florida | 15 | 0

              ...

               

              And the range table to be unioned was:

              State | Metric1 | Metric2

              NullLow | -50 | -25

              NullHigh | 200 | 50

               

              These were unioned together.

              The values for null states still showed up in color range and forced the endpoints,

              but the null states themselves only appeared on the map in the lower right indicator of "2 unknown" ,

              which can be dismissed.

               

              This method allowed for setting of separate ranges for the different metrics.

              1 of 1 people found this helpful
              • 4. Re: Define the start and end of a color spectrum legend with code
                Maryam Honari

                This workaround solves my question, though I believe there should be a way without changing data!

                Thanks anyway!