D3烛台图表日期在xaxis上

时间:2014-06-28 18:21:11

标签: javascript d3.js candlestick-chart

我正在尝试使用d3.js创建烛台图表,它工作得很好,但我无法解决问题。我确信它已被覆盖,但我找不到它。

我有烛台数据,这里有一条记录:

  {
"Date":"02/01/2010",
"Time":1265004000,
"YDay":31,
"Open":1.571500,
"High":1.582100,
"Low":1.558700,
"Close":1.580500,
"SMA":1.5663,
"EMA":1.56743786563595

},

正如您所看到的,真正的xaxis是时间值,它是纪元时间(自1/1/70以来的秒数)。我需要使用这些秒来正确格式化图表,但我希望x轴标有“Date”值而不是秒。使用此代码,情节很好,但x轴只是一组很长的数字(相互覆盖的秒数)。

如何让它在几秒钟内完成计算,但在x轴上显示日期。

这是一个包含嵌入数据的自包含html文件。只需剪切并粘贴到磁盘上的index.html,然后双击。

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>

<script>
var xs;
var ys;

var base_width = 960;
var base_height = 600;

var x_min;
var x_max;
var y_min;
var y_max;

var barWidth;
var candleWidth;

var xAxis;
var yAxis;

var margin = {top: 20, right: 30, bottom: 30, left: 40};

function getYLow (d)
{
    return ys (d.Low);
}

function getYHigh (d)
{
    return ys (d.High);
}

function getYOC (d)
{
    var yOpen = ys (d.Open);
    var yClose = ys (d.Close);

    if (yOpen < yClose)
    {
        return yOpen;
    }

    return yClose;
}

function getYHeight (d)
{
    var yHeight = ys (d.Open) - ys (d.Close);
    if (yHeight < 0)
    {
        yHeight *= -1;
    }

    return yHeight;
}

function getX (d)
{
    // offset by half the width of my candlestick rectangle
    // but centered on the actual time

    var x = xs (d.Time) - (candleWidth / 2.0);

    return x;
}

function getXActual (d)
{
    var x = xs (d.Time);

    return x;
}

function getColor (d)
{
    // I want the candlestick to be green if it closed higher
    // then the open otherwise it should be red

    if (d.Close > d.Open)
        return "green";

    return "red";
}

function load ()
{
    // load the data and create the chart

    width = base_width - margin.left - margin.right,
    height = base_height - margin.top - margin.bottom;

    ys = d3.scale.linear ().range ([height, 0]);
    xs = d3.scale.linear ().range ([0, width]);

    var svg = d3.select ("body").append ("svg")
        .attr ("width", width + margin.left + margin.right)
        .attr ("height", height + margin.top + margin.bottom)
        .style ("background-color", "black")
        .append ("g")
            .attr ("transform", "translate(" + margin.left + "," + margin.top + ")");

    xAxis = d3.svg.axis()
        .scale(xs)
        .orient("bottom");

    yAxis = d3.svg.axis()
        .scale(ys)
        .orient("left");

    y_min = d3.min (data, function (d) { return d.Low; });
    y_max = d3.max (data, function (d) { return d.High; });

    x_min = data [0].Time;
    x_max = data [data.length - 1].Time;

    // offset min and max by 1 day to give some margin so the candlestick
    // rectangle stays on the chart and does not cross the axis

    x_min = +x_min - 86400;
    x_max = +x_max + 86400;

    ys.domain ([y_min, y_max]);
    xs.domain ([x_min, x_max]);

    // this is the actual width of 1 day (1 bar)

    barWidth = xs (+x_min + 86400) - xs (+x_min);

    // the candle should be 75% the width of the day

    candleWidth = barWidth * 0.75;

    svg.append("g")
        .attr("class", "x axis")
        .attr("transform", "translate(0," + height + ")")
        .attr("stroke", "white")
        .call(xAxis);

    svg.append("g")
        .attr("class", "y axis")
        .attr("stroke", "white")
        .call(yAxis);

    svg.selectAll (".candle_bar")
        .data (data)
        .enter ().append ("rect")
              .attr ("class",  "candle_bar")
              .attr ("x",      getX)
              .attr ("y",      getYOC)
              .attr ("height", getYHeight)
              .attr ("width",  candleWidth)
              .attr ("fill",   getColor);

    svg.selectAll ("candle_hl")
        .data (data)
        .enter ().append ("svg:line")
            .attr ("x1",       getXActual)
            .attr ("y1",       getYLow)
            .attr ("x2",       getXActual)
            .attr ("y2",       getYHigh)
            .style ("stroke",  getColor);

    // for plotting an sma and ema line

    var line_sma = d3.svg.line ().x (function (d, i) { return xs (d.YDay); })
        .y (function (d, i) { return ys (d.SMA); });

    svg.append ("path")
        .attr ("d", line_sma (data))
        .attr ("stroke", "white")
        .attr ("fill", "none");

    var line_ema = d3.svg.line ().x (function (d, i) { return xs (d.YDay); })
        .y (function (d, i) { return ys (d.EMA); });

    svg.append ("path")
        .attr ("d", line_ema (data))
        .attr ("stroke", "orange")
        .attr ("fill", "none");
}

function type (d) 
{
    d.Time = +d.Time;
    d.Open = +d.Open;
    d.High = +d.High;
    d.Low = +d.Low;
    d.Close = +d.Close;
    d.SMA = +d.SMA;
    d.EMA = +d.EMA;

    return d;
}

$( document ).ready (load);

var data = [
  {
    "Date":"02/01/2010",
    "Time":1265004000,
    "YDay":31,
    "Open":1.571500,
    "High":1.582100,
    "Low":1.558700,
    "Close":1.580500,
    "SMA":1.5663,
    "EMA":1.56743786563595
  },
  {
    "Date":"02/02/2010",
    "Time":1265090400,
    "YDay":32,
    "Open":1.580500,
    "High":1.586100,
    "Low":1.572600,
    "Close":1.576900,
    "SMA":1.56758,
    "EMA":1.56933029250876
  },
  {
    "Date":"02/03/2010",
    "Time":1265176800,
    "YDay":33,
    "Open":1.576700,
    "High":1.581900,
    "Low":1.571300,
    "Close":1.576900,
    "SMA":1.56832,
    "EMA":1.57084423400701
  },
  {
    "Date":"02/04/2010",
    "Time":1265263200,
    "YDay":34,
    "Open":1.576900,
    "High":1.595000,
    "Low":1.571000,
    "Close":1.580600,
    "SMA":1.57017,
    "EMA":1.5727953872056
  },
  {
    "Date":"02/05/2010",
    "Time":1265349600,
    "YDay":35,
    "Open":1.580300,
    "High":1.586300,
    "Low":1.573100,
    "Close":1.574500,
    "SMA":1.57075,
    "EMA":1.57313630976448
  },
  {
    "Date":"02/07/2010",
    "Time":1265522400,
    "YDay":37,
    "Open":1.574300,
    "High":1.576800,
    "Low":1.571400,
    "Close":1.575100,
    "SMA":1.57224,
    "EMA":1.57352904781159
  },
  {
    "Date":"02/08/2010",
    "Time":1265608800,
    "YDay":38,
    "Open":1.575000,
    "High":1.583500,
    "Low":1.572700,
    "Close":1.577500,
    "SMA":1.5745,
    "EMA":1.57432323824927
  },
  {
    "Date":"02/09/2010",
    "Time":1265695200,
    "YDay":39,
    "Open":1.577400,
    "High":1.578200,
    "Low":1.567100,
    "Close":1.571400,
    "SMA":1.5753,
    "EMA":1.57373859059942
  },
  {
    "Date":"02/10/2010",
    "Time":1265781600,
    "YDay":40,
    "Open":1.571300,
    "High":1.573800,
    "Low":1.549800,
    "Close":1.551600,
    "SMA":1.57365,
    "EMA":1.56931087247953
  },
  {
    "Date":"02/11/2010",
    "Time":1265868000,
    "YDay":41,
    "Open":1.551700,
    "High":1.552800,
    "Low":1.533500,
    "Close":1.537500,
    "SMA":1.57025,
    "EMA":1.56294869798363
  },
  {
    "Date":"02/12/2010",
    "Time":1265954400,
    "YDay":42,
    "Open":1.537500,
    "High":1.545000,
    "Low":1.526900,
    "Close":1.535200,
    "SMA":1.56572,
    "EMA":1.5573989583869
  },
  {
    "Date":"02/14/2010",
    "Time":1266127200,
    "YDay":44,
    "Open":1.533200,
    "High":1.536100,
    "Low":1.531800,
    "Close":1.533100,
    "SMA":1.56134,
    "EMA":1.55253916670952
  },
  {
    "Date":"02/15/2010",
    "Time":1266213600,
    "YDay":45,
    "Open":1.533200,
    "High":1.535300,
    "Low":1.525500,
    "Close":1.526500,
    "SMA":1.5563,
    "EMA":1.54733133336762
  },
  {
    "Date":"02/16/2010",
    "Time":1266300000,
    "YDay":46,
    "Open":1.526500,
    "High":1.529100,
    "Low":1.519400,
    "Close":1.528000,
    "SMA":1.55104,
    "EMA":1.54346506669409
  },
  {
    "Date":"02/17/2010",
    "Time":1266386400,
    "YDay":47,
    "Open":1.527900,
    "High":1.528400,
    "Low":1.511300,
    "Close":1.515100,
    "SMA":1.5451,
    "EMA":1.53779205335528
  },
  {
    "Date":"02/18/2010",
    "Time":1266472800,
    "YDay":48,
    "Open":1.515000,
    "High":1.516100,
    "Low":1.506300,
    "Close":1.513100,
    "SMA":1.5389,
    "EMA":1.53285364268422
  },
  {
    "Date":"02/19/2010",
    "Time":1266559200,
    "YDay":49,
    "Open":1.513100,
    "High":1.516900,
    "Low":1.509500,
    "Close":1.514100,
    "SMA":1.53256,
    "EMA":1.52910291414738
  },
  {
    "Date":"02/21/2010",
    "Time":1266732000,
    "YDay":51,
    "Open":1.512400,
    "High":1.514200,
    "Low":1.510900,
    "Close":1.513200,
    "SMA":1.52674,
    "EMA":1.5259223313179
  },
  {
    "Date":"02/22/2010",
    "Time":1266818400,
    "YDay":52,
    "Open":1.513200,
    "High":1.515600,
    "Low":1.508800,
    "Close":1.511900,
    "SMA":1.52277,
    "EMA":1.52311786505432
  },
  {
    "Date":"02/23/2010",
    "Time":1266904800,
    "YDay":53,
    "Open":1.511700,
    "High":1.522000,
    "Low":1.504000,
    "Close":1.515900,
    "SMA":1.52061,
    "EMA":1.52167429204346
  },
  {
    "Date":"02/24/2010",
    "Time":1266991200,
    "YDay":54,
    "Open":1.515900,
    "High":1.526600,
    "Low":1.511100,
    "Close":1.516400,
    "SMA":1.51873,
    "EMA":1.52061943363477
  },
  {
    "Date":"02/25/2010",
    "Time":1267077600,
    "YDay":55,
    "Open":1.516200,
    "High":1.532300,
    "Low":1.514400,
    "Close":1.526500,
    "SMA":1.51807,
    "EMA":1.52179554690781
  },
  {
    "Date":"02/26/2010",
    "Time":1267164000,
    "YDay":56,
    "Open":1.526400,
    "High":1.529200,
    "Low":1.517800,
    "Close":1.522200,
    "SMA":1.51764,
    "EMA":1.52187643752625
  },
  {
    "Date":"02/28/2010",
    "Time":1267336800,
    "YDay":58,
    "Open":1.519800,
    "High":1.520400,
    "Low":1.514200,
    "Close":1.515600,
    "SMA":1.5164,
    "EMA":1.520621150021
  }
];

</script>
<body>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

Oleg,是正确的我可以使用Time Scales,我必须将我的时间从几秒转换为毫秒并且它可以工作。我现在对轴有一个不同的问题,但我会开始另一个问题,而不是链接到这个问题。

感谢Oleg我尝试时失去的部分是毫秒与秒。