<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Splunk JavaScript question - How can I compare columns and set color based on degredation? in Dashboards &amp; Visualizations</title>
    <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-JavaScript-question-How-can-I-compare-columns-and-set/m-p/293949#M18666</link>
    <description>&lt;P&gt;HI,&lt;/P&gt;

&lt;P&gt;Can you please try below XML?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;dashboard script="myjs.js" stylesheet="mycss.css"&amp;gt;
  &amp;lt;label&amp;gt;Traffic Signal&amp;lt;/label&amp;gt;
  &amp;lt;row&amp;gt;
    &amp;lt;panel&amp;gt;
      &amp;lt;table id="my_table"&amp;gt;
        &amp;lt;search&amp;gt;
          &amp;lt;query&amp;gt;| makeresults | eval count="red" | append [| makeresults | eval count="green"] | append [| makeresults | eval count="yellow"]&amp;lt;/query&amp;gt;
          &amp;lt;earliest&amp;gt;-24h@h&amp;lt;/earliest&amp;gt;
          &amp;lt;latest&amp;gt;now&amp;lt;/latest&amp;gt;
        &amp;lt;/search&amp;gt;
        &amp;lt;option name="count"&amp;gt;2&amp;lt;/option&amp;gt;
        &amp;lt;option name="drilldown"&amp;gt;none&amp;lt;/option&amp;gt;
      &amp;lt;/table&amp;gt;
    &amp;lt;/panel&amp;gt;
  &amp;lt;/row&amp;gt;
&amp;lt;/dashboard&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;myjs.js&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require([
    'underscore',
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/tableview',
    'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc, TableView) {

    var CustomRangeRenderer = TableView.BaseCellRenderer.extend({
        canRender: function(cell) {
            return _(['count']).contains(cell.field);
        },
        render: function($td, cell) {
            console.log("HI")
            if(cell.value=="red" || cell.value=="green" || cell.value=="yellow")
            {
                $td.html("&amp;lt;div class='circle_"+cell.value+"'&amp;gt;&amp;lt;/div&amp;gt;")
            }
            else if(cell.value=="NoData" || cell.value=="null")
            {
                $td.html("&amp;lt;div class='align_center'&amp;gt;&amp;lt;/div&amp;gt;")
            }
            else
            {
                $td.html("&amp;lt;div class='align_center'&amp;gt;"+cell.value+"&amp;lt;/div&amp;gt;")
            }
        }
    });

    //List of table IDs to add icon
    var tableIDs = ["my_table"];
    for (i=0;i&amp;lt;tableIDs.length;i++) {
        var sh = mvc.Components.get(tableIDs[i]);
        if(typeof(sh)!="undefined") {
            sh.getVisualization(function(tableView) {
                // Add custom cell renderer and force re-render
                tableView.table.addCellRenderer(new CustomRangeRenderer());
                tableView.table.render();
            });
        }
    }    
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;mycss.css&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; .circle_green { 
   width:1%;
   padding:10px 9px;
   margin:0 auto;
   border:0px solid #a1a1a1;
   border-radius:20px;
   background-color:rgb(7,245,7);
}
.circle_yellow { 
   width:1%;
   padding:10px 9px;
   margin:0 auto;
   border:0px solid #a1a1a1;
   border-radius:20px;
   background-color:rgb(245,245,0);
}
.circle_red { 
   width:1%;
   padding:10px 9px;
   margin:0 auto;
   border:0px solid #a1a1a1;
   border-radius:20px;
   background-color:rgb(245,7,7);
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Put JS and CSS file in below folder.&lt;/P&gt;

&lt;P&gt;SPLUNK_HOME/etc/apps/YOUR_APP/appserver/static/&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 16:52:41 GMT</pubDate>
    <dc:creator>kamlesh_vaghela</dc:creator>
    <dc:date>2020-09-29T16:52:41Z</dc:date>
    <item>
      <title>Splunk JavaScript question - How can I compare columns and set color based on degredation?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-JavaScript-question-How-can-I-compare-columns-and-set/m-p/293947#M18664</link>
      <description>&lt;P&gt;This is really more of a JS question than a splunk question I guess...&lt;/P&gt;

&lt;P&gt;I have a table where we need to compare columns and set color based on degradation.  If column 2 is 15% greater than column 1, cell level formatting should be red.  Have thresholds for each traffic light color.  I have been working with the dashboard examples and can get he basic cell level formatting figured out.  I am really stuck on the JS needed to compare column 2 to column 1 and iterate across the entire table.  &lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 18:49:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-JavaScript-question-How-can-I-compare-columns-and-set/m-p/293947#M18664</guid>
      <dc:creator>mschellhouse</dc:creator>
      <dc:date>2017-11-22T18:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk JavaScript question - How can I compare columns and set color based on degredation?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-JavaScript-question-How-can-I-compare-columns-and-set/m-p/293948#M18665</link>
      <description>&lt;P&gt;@mschellhouse, I have answered something on similar lines: &lt;A href="https://answers.splunk.com/answers/583047/can-i-color-a-cell-based-on-condition.html"&gt;https://answers.splunk.com/answers/583047/can-i-color-a-cell-based-on-condition.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;In you case you can have a Threshold % value based on which Rows will be colored. Here is the run anywhere example which you can modify as per your needs:&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/3879i70E92A5C90337FD2/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Simple XML Dashboard Code with CSS Style Inline:&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;dashboard script="table_row_highlighting_by_threshold.js"&amp;gt;
  &amp;lt;label&amp;gt;Color based on Increase percent Threshold&amp;lt;/label&amp;gt;
  &amp;lt;row&amp;gt;
    &amp;lt;panel&amp;gt;
      &amp;lt;html depends="$alwaysHideCSSHTMLPanel$"&amp;gt;
        &amp;lt;style&amp;gt;
          /* Row Coloring */

          #highlight tr td {
              background-color: #65A637 !important;
          }

          #highlight tr.range-green td {
              background-color: #65A637 !important;
          }

          #highlight tr.range-yellow td {
              background-color: #F7BC38 !important;
          }
          #highlight tr.range-red td {
              background-color: #D93F3C !important;
          }

          #highlight tr.range-grey td {
              background-color: #f3f3f3 !important;
          }

          #highlight .table td {
              border-top: 1px solid #fff;
          }
          /* 
          Apply bold font for rows with custom range colors other than Green(default).
          Bold font will ensure that jQuery is working fine to apply appropriate range Class. 
          */
          #highlight td.range-yellow, td.range-red, td.range-grey{
              font-weight: bold;
          }          
        &amp;lt;/style&amp;gt;
      &amp;lt;/html&amp;gt;
      &amp;lt;table id="highlight"&amp;gt;
        &amp;lt;search&amp;gt;
          &amp;lt;query&amp;gt;|  makeresults
|  eval Column1="100",Column2="50"
|  append
    [|  makeresults
|  eval Column1="100",Column2="120"]
|  append
    [|  makeresults
|  eval Column1="100",Column2="130"]
|  fields - _time
|  eval Threshold%=round(((Column2-Column1)/Column1)*100,1)."%"&amp;lt;/query&amp;gt;
          &amp;lt;earliest&amp;gt;-24h@h&amp;lt;/earliest&amp;gt;
          &amp;lt;latest&amp;gt;now&amp;lt;/latest&amp;gt;
          &amp;lt;sampleRatio&amp;gt;1&amp;lt;/sampleRatio&amp;gt;
        &amp;lt;/search&amp;gt;
        &amp;lt;option name="count"&amp;gt;20&amp;lt;/option&amp;gt;
        &amp;lt;option name="dataOverlayMode"&amp;gt;none&amp;lt;/option&amp;gt;
        &amp;lt;option name="drilldown"&amp;gt;none&amp;lt;/option&amp;gt;
        &amp;lt;option name="percentagesRow"&amp;gt;false&amp;lt;/option&amp;gt;
        &amp;lt;option name="refresh.display"&amp;gt;progressbar&amp;lt;/option&amp;gt;
        &amp;lt;option name="rowNumbers"&amp;gt;false&amp;lt;/option&amp;gt;
        &amp;lt;option name="totalsRow"&amp;gt;false&amp;lt;/option&amp;gt;
        &amp;lt;option name="wrap"&amp;gt;true&amp;lt;/option&amp;gt;
      &amp;lt;/table&amp;gt;
    &amp;lt;/panel&amp;gt;
  &amp;lt;/row&amp;gt;
&amp;lt;/dashboard&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;JavaScript code for &lt;CODE&gt;table_row_highlighting_by_threshold.js&lt;/CODE&gt;&lt;/STRONG&gt; &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require([
    'underscore',
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/tableview',
    'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc, TableView) {

     // Row Coloring by String Comparision of Numeric Time Value in HH:MM:SS

    var CustomRangeRenderer = TableView.BaseCellRenderer.extend({
        canRender: function(cell) {
            // Enable this custom cell renderer for Threshold% field
            return _(["Threshold%"]).contains(cell.field);
        },
        render: function($td, cell) {
            // Add a class to the cell based on the returned value percent valuePerc
            var valuePerc = cell.value;
            // Remove trailing % sign
            var value = valuePerc.substring(0, valuePerc.length - 1);
            // Convert to floating number
            var value = parseFloat(value);
            // Apply interpretation for RAG status field
            // Since we have picked only one row for applying range Class, following if is not required.
            if (cell.field === "Threshold%") {
                //Add range class based on cell values.
                if (value &amp;lt;0) {
                    $td.addClass("range-cell").addClass("range-green");
                }
                else if (value &amp;gt;=0 &amp;amp;&amp;amp; value &amp;lt;25 ) {
                    $td.addClass("range-cell").addClass("range-yellow");
                }
                else if (value &amp;gt;=25 ) {
                    $td.addClass("range-cell").addClass("range-red");
                }
                else {
                    $td.addClass("range-cell").addClass("range-grey");
                }           
            }
            // Update the cell content with percent valuePerc
            $td.text(valuePerc).addClass('string');
        }
    });

    mvc.Components.get('highlight').getVisualization(function(tableView) {

        tableView.on('rendered', function() {
            // Add a delay to ensure Custom Render applies to row without getting overridden with built in reder.           
            setTimeout(function(){  
                // Apply class of the cells to the parent row in order to color the whole row
                tableView.$el.find('td.range-cell').each(function() {
                    $(this).parents('tr').addClass(this.className);
                });
            },100);
        });

        // Add custom cell renderer, the table will re-render automatically.
        tableView.addCellRenderer(new CustomRangeRenderer());
    });
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please try out and confirm.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2017 20:26:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-JavaScript-question-How-can-I-compare-columns-and-set/m-p/293948#M18665</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-11-22T20:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk JavaScript question - How can I compare columns and set color based on degredation?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-JavaScript-question-How-can-I-compare-columns-and-set/m-p/293949#M18666</link>
      <description>&lt;P&gt;HI,&lt;/P&gt;

&lt;P&gt;Can you please try below XML?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;dashboard script="myjs.js" stylesheet="mycss.css"&amp;gt;
  &amp;lt;label&amp;gt;Traffic Signal&amp;lt;/label&amp;gt;
  &amp;lt;row&amp;gt;
    &amp;lt;panel&amp;gt;
      &amp;lt;table id="my_table"&amp;gt;
        &amp;lt;search&amp;gt;
          &amp;lt;query&amp;gt;| makeresults | eval count="red" | append [| makeresults | eval count="green"] | append [| makeresults | eval count="yellow"]&amp;lt;/query&amp;gt;
          &amp;lt;earliest&amp;gt;-24h@h&amp;lt;/earliest&amp;gt;
          &amp;lt;latest&amp;gt;now&amp;lt;/latest&amp;gt;
        &amp;lt;/search&amp;gt;
        &amp;lt;option name="count"&amp;gt;2&amp;lt;/option&amp;gt;
        &amp;lt;option name="drilldown"&amp;gt;none&amp;lt;/option&amp;gt;
      &amp;lt;/table&amp;gt;
    &amp;lt;/panel&amp;gt;
  &amp;lt;/row&amp;gt;
&amp;lt;/dashboard&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;myjs.js&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;require([
    'underscore',
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/tableview',
    'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc, TableView) {

    var CustomRangeRenderer = TableView.BaseCellRenderer.extend({
        canRender: function(cell) {
            return _(['count']).contains(cell.field);
        },
        render: function($td, cell) {
            console.log("HI")
            if(cell.value=="red" || cell.value=="green" || cell.value=="yellow")
            {
                $td.html("&amp;lt;div class='circle_"+cell.value+"'&amp;gt;&amp;lt;/div&amp;gt;")
            }
            else if(cell.value=="NoData" || cell.value=="null")
            {
                $td.html("&amp;lt;div class='align_center'&amp;gt;&amp;lt;/div&amp;gt;")
            }
            else
            {
                $td.html("&amp;lt;div class='align_center'&amp;gt;"+cell.value+"&amp;lt;/div&amp;gt;")
            }
        }
    });

    //List of table IDs to add icon
    var tableIDs = ["my_table"];
    for (i=0;i&amp;lt;tableIDs.length;i++) {
        var sh = mvc.Components.get(tableIDs[i]);
        if(typeof(sh)!="undefined") {
            sh.getVisualization(function(tableView) {
                // Add custom cell renderer and force re-render
                tableView.table.addCellRenderer(new CustomRangeRenderer());
                tableView.table.render();
            });
        }
    }    
});
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;mycss.css&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; .circle_green { 
   width:1%;
   padding:10px 9px;
   margin:0 auto;
   border:0px solid #a1a1a1;
   border-radius:20px;
   background-color:rgb(7,245,7);
}
.circle_yellow { 
   width:1%;
   padding:10px 9px;
   margin:0 auto;
   border:0px solid #a1a1a1;
   border-radius:20px;
   background-color:rgb(245,245,0);
}
.circle_red { 
   width:1%;
   padding:10px 9px;
   margin:0 auto;
   border:0px solid #a1a1a1;
   border-radius:20px;
   background-color:rgb(245,7,7);
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Put JS and CSS file in below folder.&lt;/P&gt;

&lt;P&gt;SPLUNK_HOME/etc/apps/YOUR_APP/appserver/static/&lt;/P&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 16:52:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-JavaScript-question-How-can-I-compare-columns-and-set/m-p/293949#M18666</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2020-09-29T16:52:41Z</dc:date>
    </item>
    <item>
      <title>Re: Splunk JavaScript question - How can I compare columns and set color based on degredation?</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-JavaScript-question-How-can-I-compare-columns-and-set/m-p/293950#M18667</link>
      <description>&lt;P&gt;The initial answers that I received were excellent but I didn't do a good job of describing my actual problem.  I referenced two columns for the example but I will really have multiple (10+).  Each test column will compare back to the base column and I need to color code the cell based on the % difference with respect to the base column.  &lt;/P&gt;

&lt;P&gt;Example datatable I am working with...&lt;BR /&gt;
KPI - Base - Test1 - Test2 - Test n&lt;BR /&gt;
KPI1 - 1 - 1.1 - 0.9  - 1.5&lt;BR /&gt;
KPI2 - 2 - 1.9 - 2.3 - 2.5&lt;/P&gt;

&lt;P&gt;So I would calculate the % difference between 1 and 1.1, 1 and 0.9, 1 and 1.5, etc.  I need to display the original value and just color code the cell.  I am thinking I need to store both the base value and the test value in JS somehow and calculate the % difference there.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2017 14:02:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Splunk-JavaScript-question-How-can-I-compare-columns-and-set/m-p/293950#M18667</guid>
      <dc:creator>mschellhouse</dc:creator>
      <dc:date>2017-11-27T14:02:11Z</dc:date>
    </item>
  </channel>
</rss>

