简体   繁体   English

我可以在高价位图表上使用铁本地存储和铁ajax吗

[英]Can I use iron-localstorage and iron-ajax with highcharts

I have a polymer element that uses iron-ajax to create a highchart. 我有一个使用铁ajax来创建highchart的聚合物元素。 Could I now incorporate iron-localstorage so the chart will render from the data in ls unless ls is empty in which case it will call iron-ajax to load the data from an api? 我现在是否可以合并iron-localstorage,以便图表将根据ls中的数据进行渲染,除非ls为空,在这种情况下,它将调用iron-ajax从api加载数据?

My working element is as follows: 我的工作要素如下:

    <dom-module id="sales-chart">
  <template>
    <iron-ajax id="ajax" url="{{url}}" last-response="{{data}}"></iron-ajax>
    <div id="container" style="max-width: 600px; height: 360px;"></div>
  </template>

  <script>
    Polymer({
      is: "sales-chart",

      properties: {
        url: String,
        data: Object
      },

      observers: [
        // These functions only run once the observed properties contain
        // something other than undefined.
        '_requestData(url)',
        '_chartData(data)'
      ],

      _requestData: function(url) {
        // Note: Use `generateRequest()` instead of the `auto` property
        // because `url` may not be available when your element is
        // first created.
        this.$.ajax.generateRequest();
      },

      _chartData: function (data) {
        $(this.$.container).highcharts({
          chart: {
            type: 'spline',
            renderTo: 'container'
          },

          series: [{

            data: (data.series)
          }]
        }); 


      }
    });
  </script>
</dom-module>

Something along these lines should work (did't test it tough): 遵循这些原则的东西应该起作用(不难测试):

<dom-module id="sales-chart">
  <template>
    <iron-ajax id="ajax" url="{{url}}" last-response="{{data}}"></iron-ajax>
    <div id="container" style="max-width: 600px; height: 360px;"></div>
    <iron-localstorage name="{{url}}"
        value="{{data}}"
        on-iron-localstorage-load-empty="_requestData">
    </iron-localstorage>
  </template>

  <script>
    Polymer({
      is: "sales-chart",

      properties: {
        url: String,
        data: Object
      },

      observers: [
        // These functions only run once the observed properties contain
        // something other than undefined.
        '_chartData(data)'
      ],

      _requestData: function() {
        // Note: Use `generateRequest()` instead of the `auto` property
        // because `url` may not be available when your element is
        // first created.
        this.$.ajax.generateRequest();
      },

      _chartData: function (data) {
        $(this.$.container).highcharts({
          chart: {
            type: 'spline',
            renderTo: 'container'
          },

          series: [{

            data: (data.series)
          }]
        }); 


      }
    });
  </script>
</dom-module>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM