HTML5キャンバス(実:円グラフ2ツールチップを描画)

上で次のHTML5キャンバス(戦闘:円グラフを描く)その後、私は、マウスのホバー円グラフに表示されるプロンプトボックスを追加する方法を研究しました。

プロットオブジェクト

あなたがコーディングを始める前に、私は最も簡単な方法は、パイのMouseMoveイベントの各セクションに追加され、それが移動しているとき、マウス対応するメッセージボックスがとても簡単、表示されていると考えることができます!しかし、実際には〜、この滴のようではありません

私たちの肉眼何かの作品のように見え、キャンバスと実際のHTMLElementの作品にそれらを入れていないが、我々はイベントを結合キャンバスのみを与えることができます。それでは、どのように、あなたが基準角度は、我々はそれぞれを保存する必要のある開始角度と終了角度エリア、間にある接続するために、マウスの位置と中心線を計算することができたマウスが、現在それに滞在している地域を知っています角度情報エリア。
保存を容易にするために、コンストラクタのプロットを作成します。

function Plot(start, end, color, data) {
  this.start = start;
  this.end = end;
  this.color = color;
  this.data = data;
}

あなたは、物品や円グラフの凡例領域を描画する方法に描くことができプロットプロトタイプチェーンを入れています


Plot.prototype.drawLegend = function() {
  ctx.fillRect(legend_posX, legend_posY, legend_width, legend_height);
  ctx.font = 'bold 12px Arial';
  var percent = this.data.label + ' : ' + (this.data.portion * 100).toFixed(2) + '%';
  ctx.fillText(percent, legend_textX, legend_textY);
}
Plot.prototype.drawPlot = function() {
  ctx.fillStyle = this.color;
  ctx.beginPath();
  ctx.moveTo(center.x, center.y);
  ctx.arc(center.x, center.y, radius, this.start, this.end, false);
  ctx.closePath();
  ctx.fill();
}

カスタマイズされたツールチップ

前回の記事で:HTML5キャンバス(実際の円グラフを描く)、次のように私たちの初期設計で見ることができ、コンテンツがツールチップのカスタマイズに表示することができ、ユーザーはテンプレートを設定することができます。

Year: {{year}}, Data: {{data}}

私たちの目標は、上記のテンプレートに変換することです:

Year: 2017, Data: 3000

受信ツールメソッド作成しtemplateた文字列を、マウスの現在の走行plotデータを、実際の表示は、文字列を返します。

function replaceAttr(text, data) {
    while (text.indexOf("{{") != -1) {
      var start = text.indexOf("{{"),
          end = text.indexOf("}}"),
          attr = text.substring(start + 2, end);
      text = text.replace("{{" + attr + "}}", data[attr]);
    }
    return text;
}

あなたは、コードから参照で常習的ではないことに注意してください{{}}の間にスペースを追加します。

どこマウス

地域のホバーを決定するために、我々は次の2つのステップを完了する必要があります。

  1. 算出中心位置とマウスとの間のラジアンangle
  2. トラバーサルがplots決定するangle一つに位置しているか否かplotあなたがこれを見つけた場合、間、裁判官、それはマウスが、そうであれば、示していない場合は、グラフを再描画、ツールチップを描画する必要がない最後の領域であるかどうか。対応エリアは、マウスがキャンバスエリアのパイではない、それは伝説、タイトルや空の領域を指していることを示す、見つからない場合、それはグローバル変数をクリアする必要がありますし、布を塗ってみてください。startAngleendAngleplotplotcurrentPlot

マウスの位置と中心との間にアークを決定する方法については、以下の小さなシリーズは、円グラフを描いた、ここでしか助けることができます...

function getAngle(cx, cy, mx, my) {
    var x = Math.abs(cx - mx),
        y = Math.abs(cy - my),
        z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)),
        cos = y / z,
        radina = Math.acos(cos);

    if (mx > cx && my > cy) {
      return radina;
    } else if (mx < cx && my > cy) {
      return Math.PI / 2 + radina;
    } else if (mx > cx && my < cy) {
      return 3 * Math.PI / 2 - radina
    } else {
      return 3 * Math.PI / 2 + radina
    }
}
function onMouseMove(e) {
    var ex = e.pageX - cv.offsetLeft,
        ey = e.pageY - cv.offsetTop;
    var angle = getAngle(center.x, center.y, ex, ey);
    for (let i = 0; i < plots.length; i++) {
      if (plots[i].start < angle && plots[i].end > angle) {
        if (currentPlot != plots[i]) {
          currentPlot = plots[i];
          draw();
        }
        return;
      }
    }
    currentPlot = null;
    draw();
  }

今、私たちは、マウスの滞在の現在の場所を知っても、プロンプトテキストに合わせてカスタマイズすることができ、あなたが今、プロンプトボックスを描くことができることを、次のコードは、面倒な計算過程をビットもいくつかの問題ですが、私は別の日に再計算〜

Plot.prototype.drawTooltip = function() {
    var text = replaceAttr(op.tooltip.template, this.data);
    var width_tooltipText = ctx.measureText(text).width,
        height_tooltipText = parseInt(op.tooltip.font.size, 10),
        angle = (this.start + this.end) / 2 / (2 * Math.PI) *360;
    var tan = Math.tanh(angle),
        x = 0,
        y = 0;

    if (angle < 90)((x = radius / 2 * tan + center.x) || true) && ((y = -radius / 2 + center.y) || true)
    else if (angle > 90 && angle < 180)((x = radius / 2 * tan + center.x) || true) && ((y = radius / 2 + center.y) || true)
    else if (angle > 180 && angle < 270)((x = -radius / 2 * tan + center.x) || true) && ((y = radius / 2 + center.y) || true)
    else if (angle > 270 && angle < 360)((x = -radius / 2 * tan + center.x) || true) && ((y = -radius / 2 + center.y) || true)
    var tooltip_box_x = x - radius / 4,
        tooltip_box_y = y,
        tooltip_box_width = width_tooltipText + 10,
        tooltip_box_height = height_tooltipText + 10,
        tooltip_text_x = x - radius / 4 + 5,
        tooltip_text_y = y + 10 + 2;
    ctx.fillStyle = 'white';
    ctx.fillRect(tooltip_box_x, tooltip_box_y, tooltip_box_width, tooltip_box_height);
    ctx.fillStyle = '#000';
    ctx.fillText(text, tooltip_text_x, tooltip_text_y);
}

ツールチップあなたはパイを再描画するたびに再描画する必要があるが、startAngle endAngle描画前にリセットする必要がある、あなたが描くたびに変更されます。

function clear() {
    ctx.clearRect(0, 0, cv.width, cv.height);
    startAngle = 0;
    endAngle = 0;
    cv.onmousemove = null;
}

私たちの最後のdrawメソッド〜

function draw() {
    clear();
    title_text = op.title.text;
    ctx.font = op.title.font.weight + " " + op.title.font.size + "px " + op.title.font.family;
    title_width = ctx.measureText(title_text).width;
    title_height = op.title.font.size;
    title_position = {
      x: (width, title_width) / 2,
      y: 20 + title_height
    };
    ctx.fillText(title_text, title_position.x, title_position.y);
    radius = (height - title_height - title_position.y - 20) / 2;
    center = {
      x: radius + 20,
      y: radius + 30 + title_position.y
    };
    legend_width = op.legend.font.size * 2.5;
    legend_height = op.legend.font.size * 1.2;
    legend_posX = center.x * 2 + 20;
    legend_posY = 80;
    legend_textX = legend_posX + legend_width + 5;
    legend_textY = legend_posY + op.legend.font.size * 0.9;
    ctx.strokeStyle = 'grey';
    ctx.lineWidth = 3;
    ctx.strokeRect(0, 0, width, height);

    for (var i = 0, len = data_c.length; i < len; i++) {
      endAngle += data_c[i].portion * 2 * Math.PI;
      var plot = new Plot(startAngle, endAngle, data_c[i].color, data_c[i])
      plots.push(plot);
      plot.drawPlot();
      startAngle = endAngle;
      legend_posY += (10 + legend_height);
      legend_textY += (10 + legend_height);
      plot.drawLegend();
    }
    if (currentPlot) {
      currentPlot.drawTooltip();
    }
    cv.onmousemove = onMouseMove;
}

図は、終了します:

送信元アドレス:HTTPS://github.com/Sue1024/ca ...

おすすめ

転載: www.cnblogs.com/jlfw/p/11919678.html