Splunk Dev

Need to convert values into percentage in Bar Graph

Trishant
Explorer

I have generated a chart which is having a some values, Now I want to convert those values in percentage..
I have tried every possible solution but not working for me.

Example:
Query what I used is:

sort +iteration | eval testId = testId + ": " + testcase |
rename testId as Test_CaseID, build as Build, duration as Time_Taken |
chart count(Test_CaseID) as Total_Runs over Test_CaseID by Time_Taken bins=100 |

Result i got is: alt text

So The number I am getting on the bars is a value lets say for first bar we are seeing 8, 6, 7, 2 Which means there are total 23 Total_Runs and these 23 are divided on basis of time taken..

What I need is percentage values in place of this numbers(8,6,7,2) in all bars. Is it possible to have that??

Tags (1)
0 Karma
1 Solution

somesoni2
Revered Legend

Try this

sort +iteration | eval testId = testId + ": " + testcase | 
rename testId as Test_CaseID, build as Build, duration as Time_Taken | 
chart count(Test_CaseID) as Total_Runs over Test_CaseID by Time_Taken bins=100 |
untable Test_CaseID Time_Taken count |
eventstats sum(count) as Total by Test_CaseID |
eval perc=round(count*100/Total,2) | fields - count Total
| xyseries Test_CaseID Time_Taken perc

View solution in original post

niketn
Legend

@Trishant,

Option 1
if you just want to get percent stack for each of the values of your existing chart, you can change Stack Mode from Stacked to Stacked 100%. This will show percent contribution of each stack however, will retain the Values of Stack for display.

Option 2
alt text
If you want to show percent values in the stacked chart, then you can use query similar to the following run anywhere search based on Splunk's _internal index (PS: I have used date_seconds to mimic duration).

index=_internal sourcetype=splunkd log_level!="INFO"
| chart count(component) as Total_Runs over component by date_second limit=10 useother=f
| rename * as Count*
| rename Countcomponent as component
| eval Total=0
| foreach Count* [eval Total=Total + <<FIELD>>]
| foreach Count* [eval <<FIELD>>=round((<<FIELD>>/Total)*100,1)]
| fields - Total

Your query should be something like following:

| sort +iteration 
| eval testId = testId + ": " + testcase 
| rename testId as Test_CaseID, build as Build, duration as Time_Taken 
| chart count(Test_CaseID) as Total_Runs over Test_CaseID by Time_Taken bins=100 
| rename * as Count*
| rename CountTest_CaseID  as Test_CaseID
| eval Total=0
| foreach Count* [eval Total=Total + <<FIELD>>]
| foreach Count* [eval <<FIELD>>=round((<<FIELD>>/Total)*100,1)]
| fields - Total

Following is the dashboard Simple XML for attached screenshot.
1) Chart is created with id="mychart" in order to apply white color to Data Labels using CSS
2) HTML panel is always hidden through depends attribute which refers to a token which is never set.
3) Y axis is set between 0 and 100 with interval of 10

  <row>
    <panel>
      <html depends="$alwaysHideCSSStyleHTMLPanel$">
        <style>
          #mychart svg g.highcharts-data-labels g text{
            color:#fff !important;
            fill:#fff !important;
          }
        </style>
      </html>    
      <chart id="mychart">
        <search>
          <query>index=_internal sourcetype=splunkd log_level!="INFO"
| chart count(component) as Total_Runs over component by date_second limit=10 useother=f
| rename * as Count*
| rename Countcomponent as component
| eval Total=0
| foreach Count* [eval Total=Total + &lt;&lt;FIELD&gt;&gt;]
| foreach Count* [eval &lt;&lt;FIELD&gt;&gt;=round((&lt;&lt;FIELD&gt;&gt;/Total)*100,1)]
| fields - Total</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
          <sampleRatio>1</sampleRatio>
        </search>
        <option name="charting.axisLabelsX.majorLabelStyle.overflowMode">ellipsisNone</option>
        <option name="charting.axisLabelsX.majorLabelStyle.rotation">0</option>
        <option name="charting.axisLabelsY.majorUnit">10</option>
        <option name="charting.axisTitleX.visibility">visible</option>
        <option name="charting.axisTitleY.visibility">visible</option>
        <option name="charting.axisTitleY2.visibility">visible</option>
        <option name="charting.axisX.scale">linear</option>
        <option name="charting.axisY.maximumNumber">100</option>
        <option name="charting.axisY.minimumNumber">0</option>
        <option name="charting.axisY.scale">linear</option>
        <option name="charting.axisY2.enabled">0</option>
        <option name="charting.axisY2.scale">inherit</option>
        <option name="charting.chart">column</option>
        <option name="charting.chart.bubbleMaximumSize">50</option>
        <option name="charting.chart.bubbleMinimumSize">10</option>
        <option name="charting.chart.bubbleSizeBy">area</option>
        <option name="charting.chart.nullValueMode">gaps</option>
        <option name="charting.chart.showDataLabels">all</option>
        <option name="charting.chart.sliceCollapsingThreshold">0.01</option>
        <option name="charting.chart.stackMode">stacked</option>
        <option name="charting.chart.style">shiny</option>
        <option name="charting.drilldown">none</option>
        <option name="charting.layout.splitSeries">0</option>
        <option name="charting.layout.splitSeries.allowIndependentYRanges">0</option>
        <option name="charting.legend.labelStyle.overflowMode">ellipsisMiddle</option>
        <option name="charting.legend.placement">right</option>
        <option name="height">490</option>
        <option name="trellis.enabled">0</option>
        <option name="trellis.scales.shared">1</option>
        <option name="trellis.size">medium</option>
      </chart>
    </panel>
  </row>
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

niketn
Legend

@Trishant, please accept one of the answer which has helped you with your problem, and mark the question as answered.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

somesoni2
Revered Legend

Try this

sort +iteration | eval testId = testId + ": " + testcase | 
rename testId as Test_CaseID, build as Build, duration as Time_Taken | 
chart count(Test_CaseID) as Total_Runs over Test_CaseID by Time_Taken bins=100 |
untable Test_CaseID Time_Taken count |
eventstats sum(count) as Total by Test_CaseID |
eval perc=round(count*100/Total,2) | fields - count Total
| xyseries Test_CaseID Time_Taken perc
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...