简体   繁体   English

缩放SVG图像以适合父对象并在不同情况下更改量规颜色

[英]Scale SVG image to fit parent and change gauge colour at different instances

I am trying to do the following using Raphael: 我正在尝试使用Raphael执行以下操作:

1) Scale an SVG to fit a parent div (in this case #bg ) 1)缩放SVG使其适合父级div(在本例中为#bg

2) Change the colour of the gauge at different instances... this is what I've done so far but it doesn't work: 2)在不同的情况下更改量规的颜色...这是我到目前为止所做的,但是不起作用:

archtype.customAttributes.counter = function (counter, top) {
  var motto = '';
  switch(true) {
    case(counter<=(top/10)):
      motto = 'Very Poor !'
      colour = '#BD2727'; //pretty sure this is wrong
      break;
    case(counter<=(5.61*top/10)):
      motto = 'Poor'
      colour = '#F79A38'; //pretty sure this is wrong
      break;
    case(counter<=(7.21*top/10)):
      motto = 'Fair'
      colour = '#FBDE07'; //pretty sure this is wrong
      break;    
    case(counter<=(8.81*top/10)):
      motto = 'Good'
      colour = '#90C342'; //pretty sure this is wrong
      break;
    case(counter<=(9.61*top/10)):
      motto = 'Excellent'
      colour = '#1F9C4C'; //pretty sure this is wrong
      break;       
  }
  return {
    counter: [counter,top],
    text: Math.floor(counter) + '\n' + motto
  }
}

Fiddle here: http://jsfiddle.net/mwvLc0kb/4/ 在这里提琴: http : //jsfiddle.net/mwvLc0kb/4/

I hope my answser comes not too late : 我希望我的答案来不晚:

1) fitting to the container may be tricky on IE, but is easier on Chr/FF : you will have to do the following (see my responsive gauge lib ) : 1)在IE上安装容器可能很麻烦,但在Chr / FF上则比较容易:您将必须执行以下操作(请参阅我的响应式lib ):

  • add viewBox="0 0 wh" preserveAspectRatio="xMinYMin meet" to the svg node. 向svg节点添加viewBox =“ 0 0 wh” prepareAspectRatio =“ xMinYMin Meet”。 No need for w and h to be the pixel size, they only set the ratio : for a square, w=h=1, 无需将w和h设置为像素大小,只需设置比例:对于正方形,w = h = 1,
  • add height=100% and max-width=100% to the svg node CSS, 向svg节点CSS添加height = 100%和max-width = 100%,
  • of course, set either a height or a width to the svg parent div, 当然,请为svg父div设置高度或宽度,
  • for IE, add a canvas along with the svg node, otherwise it will not size the svg properly. 对于IE,请添加一个画布以及svg节点,否则将无法正确调整svg的大小。

Note that resizing the gauge during a window resize is even more tricky on IE... 请注意,在IE上调整窗口大小期间调整仪表的大小更加棘手。

2) you had to set the color on the arc's attributes, not the counter's ones, see this fiddle . 2)您必须在圆弧的属性上设置颜色,而不是在计数器的属性上设置颜色,请参阅此小提琴

archtype.customAttributes.arc = function(xloc, yloc, value, total, R, counter, top) {
  var alpha = 360 / total * value,
  a = (90 - alpha) * Math.PI / 180,
  x = xloc + R * Math.cos(a),
  y = yloc - R * Math.sin(a),
  path;

  if (total == value) {
    path = [
      ["M", xloc, yloc - R],
      ["A", R, R, 0, 1, 1, xloc - 0.01, yloc - R]
    ];
  } else {
    path = [
      ["M", xloc, yloc - R],
      ["A", R, R, 0, +(alpha > 180), 1, x, y]
    ];
  }

  if (counter && top) {
    var colour;
    switch (true) {
      case (counter <= (top / 10)):
        colour = '#BD2727'; //pretty sure this is wrong
        break;
      case (counter <= (5.61 * top / 10)):
        colour = '#F79A38'; //pretty sure this is wrong
        break;
      case (counter <= (7.21 * top / 10)):
        colour = '#FBDE07'; //pretty sure this is wrong
        break;
      case (counter <= (8.81 * top / 10)):
        colour = '#90C342'; //pretty sure this is wrong
        break;
      case (counter <= (9.61 * top / 10)):
        colour = '#1F9C4C'; //pretty sure this is wrong
        break;
    }
  }

  var result = {
    path: path
  };
  if (colour){
    result.stroke = colour;
  }

  return result;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM