在弯曲的路径上移动对象

时间:2015-09-24 21:26:31

标签: c# unity3d

我使用这个脚本来移动Arch中的对象,但我遇到的问题是如果其中一个Y值为(开始和/或结束)为负,则对象向下弯曲并且周围。无论Y值是多少,我想要的是它总是曲线上升。我怎么能实现这个目标呢?

var data = [
        { "date": "2012-03-20", "total": 3, "initiative": 0 },
        { "date": "2012-03-21", "total": 8, "initiative": 0 },
        { "date": "2012-03-22", "total": 2, "initiative": 1 },
        { "date": "2012-03-23", "total": 10, "initiative": 1 },
        { "date": "2012-03-24", "total": 3, "initiative": 2 },
        { "date": "2012-03-25", "total": 20, "initiative": 2 }
    ];

    var ordinals = ["a", "b", "c"];

    var margin = { top: 40, right: 40, bottom: 40, left: 40 },
        width = 600,
        height = 500;

    var x = d3.time.scale()
        .domain([new Date(data[0].date), d3.time.day.offset(new Date(data[data.length - 1].date), 1)])
        .rangeRound([0, width - margin.left - margin.right]);

    var y = d3.scale.ordinal()
        .domain(ordinals)
        .rangePoints([height - margin.top - margin.bottom, 0]);

    var xAxis = d3.svg.axis()
        .scale(x)
        .orient('bottom')
        .ticks(d3.time.days, 1)
        .tickFormat(d3.time.format('%a %d'))
        .tickSize(0)
        .tickPadding(8);

    var yAxis = d3.svg.axis()
        .scale(y)
        .orient('left')
        .tickPadding(8);

    var zoom = d3.behavior.zoom()
        .x(x)
        .y(y)
        .scaleExtent([1, 4])
        .on("zoom", zoomed);

    var svg = d3.select('body').append('svg')
        .attr('class', 'chart')
        .attr('width', width)
        .attr('height', height)
        .attr('transform', 'translate(' + margin.left + ',' + margin.bottom + ')')
        .call(zoom);

    svg.append("rect")
        .attr('fill', 'none')
        .attr('stroke', 'blue')
        .attr("width", width)
        .attr("height", height);

    svg.append('g')
        .attr('class', 'x axis')
        .attr('transform', 'translate(' + margin.left + ',' + (height - margin.top) + ')')
        .call(xAxis);

    svg.append('g')
        .attr('class', 'y axis')
        .attr('transform', 'translate(' + margin.left + ',' + margin.bottom + ')')
        .call(yAxis);

    map = svg.selectAll('.chart')
        .data(data)
        .enter().append('rect')
        .attr('class', 'bar')
        .attr('x', function (d) { return x(new Date(d.date)) + margin.left; })
        .attr('y', function (d) { return y(ordinals[d.initiative]) + margin.bottom; })
        .attr('width', 50)
        .attr('height', 25);

    var clip = map.append("defs").append("clipPath")
        .attr("id", "clip")
        .append("rect")
        .attr("id", "clip-rect")
        .attr('stroke', 'red')
        .attr("width", width - margin.right - margin.left)
        .attr("height", height - margin.top - margin.bottom)
        .attr('transform', 'translate(' + margin.left + ',' + margin.bottom + ')');

    var zoom = d3.behavior.zoom()
        .x(x)
        .y(y)
        .scaleExtent([1, 4])
        .on("zoom", zoom);

    function zoomed() {

        svg.select(".x.axis").call(xAxis);
        svg.select(".y.axis").call(yAxis);
        map.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
        map.attr("clip-path", "url(#clip)");

0 个答案:

没有答案