chart.js 再描画でのエラーを回避


ここで教えて頂きました。

< コード例 >

チャートは4個を同時に表示
chart.jsの描画はクラスで行ってます。
4個のチャートオブジェクトは、staticな配列に入れてます。


static charts = [null, null, null, null] 

   // 前回消去 //
    if (typeof PieChart.charts[this.idx] !== 'undefined' &&  PieChart.charts[this.idx] !== null && PieChart.charts[this.idx]) {
      PieChart.charts[this.idx].destroy();
    }


    // == ドーナツチャート描画 == //
    PieChart.charts[this.idx] = new Chart(context, {
      //type: 'doughnut',
      //type: 'pie',
      type: chartType,
      data: {
        labels: labels,
        datasets: [{
            label: name,
            backgroundColor: ["#fa8072", "#00ff7f", "#00bfff", "#a9a9a9", "#f5f5f5", "#a7d4d4", "#E5E100"],
            data: counts
          }]
      },

      // プラグインをセット //
      plugins: [
        ChartDataLabels
      ],

      //color: "#ffffff",
      options: {
        responsive: false,
        maintainAspectRatio: false, // 2回目以降大きくなってしまうのを防止用

        legend: {
          labels: {
            // グローバルプロパティを上書 ?????? no affect //
            fontColor: 'white'
          }
        },

        // データラベルがアフェクトする用 //
        /*toltip: {
         enabled: false
         },*/

        plugins: {

          // データラベルがアフェクトする用 //
          toltip: {
            enabled: false
          },

          title: {
            display: true,
            text: name
          },

          // == データラベルプラグインの設定 == //
          datalabels: {
            color: '#294829',
            font: {
              weight: 'bold',
              size: 10, // <=== 小さ目
            },
            formatter: (value, ctx) => {

              let label = ctx.chart.data.labels[ctx.dataIndex];
              //return label + '\n' + value + '%';
              return label
            }
          }
          // == データラベルプラグインの設定終わり == //

        }
      }
    })

Tabulator カラムのグループヘッダー

 

 

 

ドキュメントはここ

< コード例 >

グループ化したいタイトルのカラムの配列を入れ子にします。


      // == Sizes == //
      {
        title: "Sizes",
        columns: [
          // GB Size //
          {title: 'Size (GB)', field: 'GB_SIZE', width: 50, hozAlign: 'right',
            formatter: function (cell, formatterParams, onRendered) {
              return numeral(cell.getValue()).format("#,###")
            }
          },

          // GB 前日//
          {title: 'Size (GB) P', field: 'GB_SIZE_BF1', width: 50, hozAlign: 'right',
            formatter: function (cell, formatterParams, onRendered) {
              return numeral(cell.getValue()).format("#,###")
            }
          },

          // MB Diff from 1 day before //
          {title: 'DiffS', field: 'DIFF_MB_SIZE', width: 60, hozAlign: 'right',
            formatter: function (cell, formatterParams, onRendered) {
              return numeral(cell.getValue()).format("#,###")
            }
          },
        ]
      },

      // == Counts == //
      {
        title: "Counts",
        columns: [

          // File count //
          {title: 'Count', field: 'CNT_FILE', width: 70, hozAlign: 'right',
            formatter: function (cell, formatterParams, onRendered) {
              return numeral(cell.getValue()).format("#,###")
            }
          },
          // File count previous //
          {title: 'Count P', field: 'CNT_FILE_BF1', width: 70, hozAlign: 'right',
            formatter: function (cell, formatterParams, onRendered) {
              return numeral(cell.getValue()).format("#,###")
            }
          },

          // File count difference from previous date //
          {title: 'DiffC', field: 'DIFF_CNT_FILE', width: 60, hozAlign: 'right',
            formatter: function (cell, formatterParams, onRendered) {
              return numeral(cell.getValue()).format("#,###")
            }
          }
        ]
      },

< 画面例 >

Leaflet Javascript AlmostOver でポリラインのイベント

Leaflet Javascriptで、GeoJSONから読み込んだポリラインに対してイベントをつける必要があり、
mouseover, mouseoutのイベントが発火しにくいので、調べてましたところ AlmostOver のプラグインで解決しました。

< コード例 >

HTML


<!-- Leaflet almostover -->
<script type="text/javascript" src="Script/js/leaflet.almostover.js"></script>

Javascript


        // almost over //
        lmap.almostOver.addLayer(DrawLine.amlinejson);

        // @@ 線をクリックした時 @@ //
        lmap.on('almost:click', function (e) {
         
         if (DrawLine.thickLines) {
         lmap.removeLayer(DrawLine.thickLines)
         }
         // if (RailLines.stationsjson) {
         //   lmap.removeLayer(RailLines.stationsjson)
         // }
         
         
         //console.log("--- e.target")
         //console.log(e.target)
         
         const layer = e.layer
         const pp = layer.feature.properties
         
         // 線を太くする //
         const tgtjson = new getComlineJSON(datas, pp.NAME)
         //console.log(tgtjson)
         
         DrawLine.thickLines = L.geoJson(tgtjson, {
         
         // スタイル //
         style: {color: "#00FFFF", weight: 7}
         
         }).addTo(lmap)
         
         //const comline = pp.NM_COM + " " + pp.NM_LINE
         //$("#raillinespn").text(comline)
         
         // リクエスト引数リスト名 //
         //const listnm = "国交 " + pp.NM_COM + " " + pp.NM_LINE
         //alert(listnm)
         
         // 駅簡略化マーカー描画 //
         //const rl = new RailLines()
         //rl.getStationsMarker(listnm)
         
         });

< 画面例 >

マウスが乗った線を太くする

国土交通省 国土数値情報

飛行機のライブ地図で空港の境界線もわかれば面白いので、探してましたところ、国土交通省・国土数値情報・空港データでできることがわかり使ってみました。

https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-C28-v2_4.html

< コード例 >

直接GeoJSONを読み込むわけではなく、データベースに問い合わせて対象リストのGeoJSONを生成するAPIを使ってますので、長くなるので省略

< 画面例 >

Leaflet JavaScript OpenRailwayMap

Leafletの地図に鉄道線路、駅を表示する必要があり、探しましたところベースレイヤーでOpenRailwayMapというのが見つかり使ってみました。

 

< コード例 >

レイヤー選択にOpenRailwayMapを加える


{
        label: '鉄道路線図',
        url: 'http://{s}.tiles.openrailwaymap.org/standard/{z}/{x}/{y}.png',
        attr: {
          minZoom: 2,
          maxZoom: 19,
          tileSize: 256,
          attribution: '<a href="https://www.openstreetmap.org/copyright">© OpenStreetMap contributors</a>, Style: <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA 2.0</a> <a href="http://www.openrailwaymap.org/">OpenRailwayMap</a> and OpenStreetMap'
        },
        ldtp: "L"
      },

OpenStreetMapを重ねる必要があります。


   let baseMaps = {};
    let curkey = "";
    let objlayer;
    $.each(leyers, function (j, itm) {
      curkey = itm["label"];
      objlayer = L.tileLayer(itm["url"], itm["attr"]);
      baseMaps[curkey] = objlayer;
    });

    // layersコントロールにbaseMapsオブジェクトを設定して地図に追加
    // コントロール内にプロパティ名が表示される

    L.control.layers(baseMaps).addTo(lmap);
   
    let tgtbase = baseMaps[layerkey];
    tgtbase.addTo(lmap);

    //baseMaps["OpenRailwayMap"].addTo(lmap)

    // 鉄道地図の場合、OpenStreetmapも重ねる必要あるので //
    if (layerkey.includes("OpenRailwayMap")) {
      const addmap = layerkey.includes("satellite") ? "Google satellite" : "Open street map"
      const osbase = baseMaps[addmap]
      try {
        osbase.addTo(lmap);
      }
      catch (e) {
      }

    }
    else {
      $("#legend").hide()
    }

< 画面例 >

Leaflet JavaScript マウスオーバーでポリラインを強調

鉄道路線図が入った地図で、ポリラインのセグメントにマウスが乗った時、その線を強調させてみました。

< コード例 >


    let thickLines

    // -- フィルターされたJSONのfeaturesを走査 -- //  
    const geojson = L.geoJson(json, {
      // 非強調、通常のスタイル //
      style: function (feature) {
        return {color: linecol, weight: 2.0}
      },
      onEachFeature: function (feature, layer) {

        const pp = feature.properties

        // @@ マウスが乗った時 @@ //
        layer.on('mouseover', function (e) {


          // 線を太くする //
          const tgtjson = new RailLines().getComlineJSON(json, pp.NM_COM, pp.NM_LINE)
          //console.log(tgtjson)

          thickLines = L.geoJson(tgtjson, {
            style: {color: linecol, weight: 7}
          }).addTo(lmap)

          const comline = pp.NM_COM + " " + pp.NM_LINE
          $("#raillinespn").text(comline /*+ " " + tgtjson["distKM"] + " Km"*/)


        })

        // @@ 乗ったマウスが外れた時 @@ //
        layer.on('mouseout', function (e) {
          //$("#raillinespn").text("")

          // 線を元に戻す //
          lmap.removeLayer(thickLines)

        })

      },
      pointToLayer: function (feature, latlng) {
      },
    })
    // マップに追加 //
    geojson.addTo(lmap);
    // 消去用配列に追加 //
    RailLines.lines.push(geojson)

  /**
   * 指定路線全線強調用GeoJSON取得
   * @param {type} json 全路線JSON
   * @param {type} com 会社名
   * @param {type} line 路線名
   * @returns {RailLines.getComlineJSON.raillinesAnonym$0} 連想配列
   */
  getComlineJSON(json, com, line) {

    const fts = json["features"]
    const tf = new MyTurf()

    let resfts = []
    let ttldst = 0

    fts.forEach((ft, i) => {
      const pp = ft.properties
      if (pp.NM_COM === com && pp.NM_LINE === line) {
        resfts.push(ft)


        const coords = ft.geometry.coordinates
        coords.forEach((cd, k) => {
          if (k > 0) {
            ttldst += tf.getDistanceM(cd, coords[k - 1])
          }
        })
        
      }
    })
    const distKM = Math.round(ttldst / 1000 * 100) / 100  // ???? 短い

    return {
      type: "FeatureCollection",
      features: resfts,
      distKM: distKM
    }

  }

< 画面例 >