Spotfire Javascript Output Disappeared

1k views Asked by At

I have a small javascript in a spotfire Text area. It was working as designed. I saved and closed spotfire. On re-open it does not show at all. My code is below. Any help would be appreciated.

resource = [
  "//cdn.rawgit.com/toorshia/justgage/master/raphael-2.1.4.min.js",
  "//cdnjs.cloudflare.com/ajax/libs/justgage/1.2.8/justgage.js"
]

//add scripts to head
$.getScript(resource[0], function() {
  $.getScript(resource[1], init)
})

var init = function() {
  var g = new JustGage({
    id: "gauge",
    min: 0,
    max: 100,
    customSectors: [{
        "lo": 0,
        "hi": 89.999,
        "color": "#f05050"
      },
      {
        "lo": 90,
        "hi": 92.999,
        "color": "#DD7502"
      },
      {
        "lo": 93,
        "hi": 100,
        "color": "#41c572"
      }
    ],
    levelColorsGradient: false
  });
  //refresh gauge when calcvalue changes
  $(calcValue).on('DOMSubtreeModified', function() {
    g.refresh($(this).text())
  })
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id=calcValue><SpotfireControl id="3321e4c9003142ad83fdb753e6f66605" />
</span>
<DIV id=gauge></DIV>

2

There are 2 answers

0
epascarello On BEST ANSWER

I looked at the examples, it should be:

resource = [
  "//cdn.rawgit.com/toorshia/justgage/master/raphael-2.1.4.min.js",
  "//cdnjs.cloudflare.com/ajax/libs/justgage/1.2.8/justgage.js"
]

//add scripts to head
$.getScript(resource[0], function() {
  $.getScript(resource[1], init)
})

var init = function() {
  var g = new JustGage({
    id: "gauge",
    min: 0,
    max: 100,
    customSectors: {ranges: [{
        "lo": 0,
        "hi": 89.999,
        "color": "#f05050"
      },
      {
        "lo": 90,
        "hi": 92.999,
        "color": "#DD7502"
      },
      {
        "lo": 93,
        "hi": 100,
        "color": "#41c572"
      }
    ]},
    levelColorsGradient: false
  });
  //refresh gauge when calcvalue changes
  //$(calcValue).on('DOMSubtreeModified', function() {
  //  g.refresh($(this).text())
  //})
  window.setInterval( () => {
    g.refresh(Math.random()*100)
  }, 1000)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id=calcValue><SpotfireControl id="3321e4c9003142ad83fdb753e6f66605" />
</span>
<DIV id=gauge></DIV>

0
jinana3 On

Our JustGage set up is a little different but we ran into the same issue of calculation not showing up when the dashboard is opened on web browser etc.

The problem (at least what I think) is asynchronous code. The original code is this:

    resource=[
     "//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js",
     "//cdnjs.cloudflare.com/ajax/libs/justgage/1.2.9/justgage.js"
    ]


    $.getScript(resource[0],function(){
     $.getScript(resource[1],init3)
    })


    //init3 function generates the gauge chart
    init3 =function(){
      var g3 = new JustGage({
        id: "gaugeIRF", 
        value:  $('#calcValueIRF').text(), 
        min: 0,
        max: 100,
        levelColors : [  "#73CC06", "#dec121",  "#dec121", "#bd5537" ],
        levelColorsGradient: false,
        symbol:'%',
        pointer: true,
        gaugeWidthScale: 0.6,
        counter: true,
        relativeGaugeSize: true,
        title: "IRF Readmission Rate"
      });

      //refresh gauge when calcvalue changes
      $('#calcValueIRF').on('DOMSubtreeModified',function(){
        g3.refresh($(this).text())
      })
    }

The init3 function will run and when the browser reads init3, the CalcValue will calculate but likely init3 will finish running before CalcValue is able to give a number back. This is asynchronous and the two processes do not finish in the right order. This is why when the browser opens or you come back to the dashboard, the calculation does not show up.

The solution is to put a function in place. Functions in functions run in the correct order for JavaScript. Here's the working code for me:

    resource=[
     "//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.4/raphael-min.js",
     "//cdnjs.cloudflare.com/ajax/libs/justgage/1.2.9/justgage.js"
    ]


    $.getScript(resource[0],function(){
     $.getScript(resource[1],init3)
    })


    //new function to get the calculated value
    function getValue3(){
      var newValue = $('#calcValueIRF').text()
      return newValue
    }


    init3 =function(){
      var g3 = new JustGage({
        id: "gaugeIRF", 
        value:  getValue3(), 
        min: 0,
        max: 100,
        levelColors : [  "#73CC06", "#dec121",  "#dec121", "#bd5537" ],
        levelColorsGradient: false,
        symbol:'%',
        pointer: true,
        gaugeWidthScale: 0.6,
        counter: true,
        relativeGaugeSize: true,
        title: "IRF Readmission Rate"
      });

      //refresh gauge when calcvalue changes
      $('#calcValueIRF').on('DOMSubtreeModified',function(){
        g3.refresh($(this).text())
      })
    }