You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Chart.js关联的循环函数内对匹配数组项的值求和?

解决点击事件中针对当前项求和的问题

我来帮你搞定这个求和的问题!你现在遇到的核心问题是没有在匹配到当前行政区(Comune)的arrayItem上下文里计算求和,导致拿到的是遍历整个myJSON后的全局总和,而不是当前点击区域对应的数据总和。

具体修改步骤

  1. 在匹配的if块内计算当前项的和:
    就在if (arrayItem["Comune"] == e.target.feature.properties.NOME_COM.toUpperCase()) {这个代码块的最开头,添加针对当前arrayItem的求和逻辑。如果你的Value 1和Value 2是字符串类型,记得先转成数字,避免字符串拼接:

    // 计算当前匹配项的Value 1 + Value 2总和
    // 如果是字符串类型,用parseInt/parseFloat转成数字
    const sumValues = parseInt(arrayItem['Value 1'], 10) + parseInt(arrayItem['Value 2'], 10);
    

    如果这两个值本身就是数字类型,直接相加即可:

    const sumValues = arrayItem['Value 1'] + arrayItem['Value 2'];
    
  2. 把总和填入datiedu2的data数组:
    修改datiedu2的定义,把刚才计算的sumValues放进去:

    var datiedu2 = {
      "labels": ['Lavori non terminati', 'Lavori in corso'],
      "datasets": [{
        label: 'Numero',
        data: [sumValues], // 这里放入当前项的求和结果
        backgroundColor: 'rgb(255, 99, 132)',
        borderWidth: 1
      }]
    };
    

修改后的完整代码片段

这里是整合后的关键代码部分:

click: function(e) { 
  map.fitBounds(e.target.getBounds()); 
  myJSON.forEach(function (arrayItem) { 
    if (arrayItem["Comune"] == e.target.feature.properties.NOME_COM.toUpperCase()) { 
      // --- 新增的求和逻辑 ---
      const sumValues = parseInt(arrayItem['Value 1'], 10) + parseInt(arrayItem['Value 2'], 10);
      // ---------------------

      $('.lead').html(''); 
      $('.lead').html('Comune di ' + arrayItem.Comune); 
      $('#pulsante').html(''); 
      $('#pulsante').append('<div id="emptyred"><span style="opacity:0;">FMVPAFSB</span></div>') 
      $('#pulsante').append('<div class="row" id="redditiButton"><div class="col-lg-6"><form><label class="radio-inline btn btn-danger"><input type="radio" id="generale" value="generale" name="radiored" autocomplete="off" checked> Generale</label><label class="radio-inline btn btn-danger"><input type="radio" id="dettaglio" value="dettaglio" name="radiored" autocomplete="off"> Dettaglio</label><label class="radio-inline btn btn-danger"><input type="radio" id="grafico3" value="grafico3" name="radiored" autocomplete="off"> Grafico3</label></form></div>') 
      $('#chartContainer').append('<canvas id="Chartedu"><canvas>') 

      var datiedu = { 
        "labels": ['Lavori non terminati', 'Lavori in corso', 'Lavori terminati'], 
        "datasets": [{ 
          label: 'Numero', 
          data: [arrayItem['Value 1'], arrayItem['Value 2'], arrayItem['Value 3']], 
          backgroundColor: ['rgb(255, 99, 132)', 'rgb(255, 51, 95)', 'rgb(255, 0, 55)'], 
          borderWidth: 1 
        }] 
      }; 

      var datiedu2 = { 
        "labels": ['Lavori non terminati', 'Lavori in corso'], 
        "datasets": [{ 
          label: 'Numero', 
          data: [sumValues], // 这里使用当前项的求和结果
          backgroundColor: 'rgb(255, 99, 132)', 
          borderWidth: 1 
        }] 
      }; 

      function grafo(dati, opzioni) { 
        var grafobase = document.getElementById('Chartedu').getContext('2d'); 
        new Chart(grafobase, { type: 'doughnut', data: dati, options: opzioni }); 
      }; 

      grafo(datiedu); 
    } 
  }); 
}

为什么之前会出错?

之前你可能是在myJSON.forEach循环外面计算了总和,或者在循环里没有限制只对当前匹配的项求和,导致累加了所有myJSON项的Value 1和Value 2,最终得到全局总和。现在把求和逻辑放到if内部,只针对当前点击匹配的那个arrayItem计算,就能得到对应行政区的正确数据总和了。

内容的提问来源于stack exchange,提问作者Anna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 20:32:48