需要颜色下面的线条图纯html和CSS

时间:2017-04-25 01:28:22

标签: javascript jquery html css

我正在创建一个包含HTML和CSS的折线图。代码片段来自互联网。

我得到了我需要的折线图。唯一剩下的就是图表中线条下面的背景颜色。

This is what I need:

This is what I currently have:

这就是我需要的:(图1) 这就是我目前所拥有的:(图2) 有没有什么方法可以用HTML和CSS实现这个,或者我们可以用JS做到这一点?任何帮助都将受到高度赞赏。

HTML:

                <div class="line-chart-block block">
 <div class="line-chart">
   <div class='grafico'>
   <ul class='eje-y'>
     <li data-ejeY='415'></li>
     <li data-ejeY='207'></li>
     <li data-ejeY=''></li>
   </ul>
   <ul class='eje-x'>
     <li>6/10</li>
     <li>6/11</li>
     <li>6/12</li>
     <li>6/13</li>
     <li>6/14</li>
     <li>6/15</li>
     <li>Today</li>
   </ul>
     <span data-valor='25'>
       <span data-valor='8'>
         <span data-valor='13'>
           <span data-valor='5'>   
             <span data-valor='23'>   
             <span data-valor='12'>
                 <span data-valor='15'>
                 </span></span></span></span></span></span></span>
   </div>

              </div>
            </div>

CSS:

        .line-chart-block, .bar-chart-block {
            }
                .line-chart {
                    height: 500px;
                    background: transparent;
                }
                .time-lenght {
                    padding-top: 22px;
                    padding-left: 38px;
          overflow: hidden;
                }
                    .time-lenght-btn {
                        display: block;
                        width: 70px;
                        line-height: 32px;
                        background: #50597b;
                        border-radius: 5px;
                        font-size: 14px;
                        text-align: center;
                        margin-right: 5px;
                        -webkit-transition: background .3s;
                        transition: background .3s;
                    }
                        .time-lenght-btn:hover {
                            text-decoration: none;
                            background: #e64c65;
                        }
                .month-data {
                    padding-top: 28px;
                }
                    .month-data p {
                        display: inline-block;
                        margin: 0;
                        padding: 0 25px 15px;            
                        font-size: 16px;
                    }
                        .month-data p:last-child {
                            padding: 0 25px;
              float: right;
                            font-size: 15px;
                        }
                        .increment {
                            color: #e64c65;
                        }

.grafico {
  padding: 2rem 1rem 1rem;
  width: 100%;
  height: 100%;
  position: relative;
  color: #fff;
  font-size: 80%;
}
.grafico span {
display: block;
    position: absolute;
    bottom: 20.6rem;
    left: 8rem;
    height: 0;
    border-top: 2px solid;
    transform-origin: left center;
}
.grafico span > span {
  left: 100%; bottom: 0;
}
[data-valor='25'] {width: 3px; transform: rotate(-45deg);}
[data-valor='8'] {width: 105px; transform: rotate(20deg);}
[data-valor='13'] {width: 114px; transform: rotate(7deg);}
[data-valor='5'] {width: 125px; transform: rotate(40deg);}
[data-valor='23'] {width: 69px; transform: rotate(-70deg);}
[data-valor='12'] {width: 36px; transform: rotate(75deg);}
[data-valor='15'] {width: 45px; transform: rotate(-45deg);}

[data-valor]:before {
  content: '';
  position: absolute;
  display: block;
  right: -4px;
  bottom: -3px;
  padding: 4px;
  background: red;
  border-radius: 50%;
}

[class^='eje-'] {
  position: absolute;
  left: 0;
  bottom: 0rem;
  width: 100%;
  padding: 1rem 1rem 0 2rem;
  height: 80%;
}
.eje-x {
  height: 20rem;
}
.eje-y li {
  height: 25%;
  border-top: 1px solid #777;
    list-style: none;
    color: #C4C4C4;
}
[data-ejeY]:before {
  content: attr(data-ejeY);
  display: inline-block;
  width: 2rem;
  text-align: right;
  line-height: 0;
  position: relative;
      left: 0.5rem;
    top: 0.5rem;
} 
.eje-x li {
  width: 14%;
  float: left;
  text-align: center;
    list-style: none;
    color: #C4C4C4;
}

还有一些额外的CSS,所以请忽略它。

1 个答案:

答案 0 :(得分:1)

由于您不想要JS,我建议您使用SVG,除非您需要旧的浏览器支持。

SVG browser support

Someone's example on codepen, pretty close to yours.

来自上述例子的代码:

&#13;
&#13;
body {
  /* background: #ccc; */
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

body, html {
  height: 100%;
}
.graph-container {
  position: relative;
}
.pointer-bubble {
  position: relative;
  left: 10.2em;
  bottom: -3.5em;
  opacity: 0.9;
}
.chart {
  background: white;
  width: 500px;
  height: 200px;
  border-left: 1px dotted #555;
  border-bottom: 1px dotted #555;
  padding: 20px 20px 20px 0;
}
.x-labels, .y-labels {
  color: gray;
  font-size: 0.7em;
}
.x-labels {
  position: relative;
  bottom: 0px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
.may6{
  margin-left: 13em;
}
.may15 {
  margin-left: 19em;
}
.y-labels {
  position: relative;
  bottom: 19em;
  right: 64em;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}
.y-label.spacer {
  height: 8em;
}
#pointer {
   width: 120px;
   height: 80px;
   background: red;
   position: relative;
   -moz-border-radius:    10px;
   -webkit-border-radius: 10px;
   border-radius:         10px;
}
#pointer:before {
   content:"";
   position: absolute;
   right: 100%;
   top: 26px;
   width: 0;
   height: 0;
   border-top: 13px solid transparent;
   border-right: 26px solid red;
   border-bottom: 13px solid transparent;
}
.pointer {
  color: #ffffff;
}
p#may9 {
  margin: 0 0;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 0.8em;
}
p#viewer-count {
  margin: 0.3em 0 0 0;
  font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
  font-size: 0.9em;
}
.pointer-bubble {
  border-radius: 4px;
  text-align: center;
  position: relative;
  width: 8em;
  height: 2em;
  padding: 10px;
  color: #000;
  background: #5b5b5b;
}
.pointer-bubble:after {
  content:"";
  position: absolute;
  bottom: -10px;
  left: 4em;
  border-width: 10px 10px 0; /* vary these values to change the angle of the vertex */
  border-style: solid;
  border-color: #5b5b5b transparent;
  display: block;
  width:0;
}
&#13;
<div class="graph-container">
  <div class="pointer-bubble">
    <p class="pointer" id="may9">Monday, May 9</p>
    <p class="pointer" id="viewer-count">56 Viewers</p>
  </div>
  <div class="chart-box">
    <svg viewBox="0 0 510 200" class="chart">
     <defs>
      <marker id="circle" markerWidth="4" markerHeight="4" refx="2" refy="2">
        <circle cx="2" cy="2" r="2" stroke="none" fill="#3887cc"/>
    </marker>
  </defs>
<polygon
    fill="#eef3f7"
    stroke="none"
    stroke-width="4"
    points="
            0, 72,
4, 72,
30, 105,
34, 105,
60, 72,
64, 72,
90, 82,
94, 82,
120, 57,
124, 57,
150, 51,
154, 51,
180, 45,
184, 45,
210, 81,
214, 81,
240, 64,
244, 64,
270, 87,
274, 87,
300, 63,
304, 63,
330, 33,
334, 33,
360, 69,
364, 69,
390, 58,
394, 58,
420, 105,
424, 105,
450, 97,
454, 97,
480, 57,
484, 57,
510, 70,
514, 70,
514, 220,
0, 220"
/>
<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-start="url(#circle)"
    marker-end="url(#circle)"
    points="
   4, 72
   30, 105
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   34, 105
   60, 72
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   64, 72
   90, 82
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   94, 82
   120, 57
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   124, 57
   150, 51
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   154, 51
   180, 45
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   184, 45
   210, 81
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   214, 81
   240, 64
    "
    />
<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   244, 64
   270, 87
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   274, 87
   300, 63
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   304, 63
   330, 33
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   334, 33
   360, 69
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   364, 69
   390, 58
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   394, 58
   420, 105
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   424, 105
   450, 97
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   454, 97
   480, 57
    "
    />

<polyline
    fill="none"
    stroke="#cad7e3"
    stroke-width="2"
    marker-end="url(#circle)"
    points="
   484, 57
   510, 70
    "
    />
  </svg>
    <div class="x-labels">
      <div class="may1">May 1</div>
      <div class="may6">May 6</div>
      <div class="may15">May 15</div>
    </dev>
    <div class="y-labels">
      <div class="y-label fifty">50</div>
      <div class="y-label spacer"></div>
      <div class="y-label fifteen">15</div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;