threejs 饼图+圆环+柱状图+雷达扫光

地址:https://www.wellyyss.cn/demo-3d/#/index7

下面贴出主要shader代码

 

饼图

  function getStr(data) {
        let str = `if(0.0 <= ag && ag <= angle[0]) {
           color1 = color[0];
        }`
        for (let i = 1; i < data.length; i++) {
          str = str + `else if(angle[${i - 1}]<=ag && ag <= angle[${i}]) {
             color1 = color[${i}];
          }`
        }
        return str
      }

      const H = 200
      const shader = {
        vs: `
      varying vec3 vp;
   void main()
   {
        vp = position;
    vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
    gl_Position = projectionMatrix * mvPosition;
   }`,
        fs: `
        varying vec3 vp;
        vec3 color1;
        float ag;
        uniform vec3 color[${data.length}];
        uniform float divide[${data.length}];
        uniform float angle[${data.length}];
        void main( void ) {
        ag = atan(vp.x, vp.z) - atan(1.0, 0.0); // atan(x/y) 返回的是-pi/2 -- pi/2  atan(x, y) 返回的是 -pi -- pi
        if (ag < 0.0) ag += 2.0 * 3.1415926;
        ${getStr(data)}
        gl_FragColor = vec4( color1.r,  color1.g,  color1.b, 1.0 );

    }`,
        uniforms: {
          color: {
            type: 'l',
            value: [
              // new THREE.Color('red'),
              // new THREE.Color('yellow'),
              // new THREE.Color('blue'),
              // new THREE.Color('green')
            ]
          },
          angle: {
            type: 'l',
            value: []
          },
          divide: {
            type: 'l',
            value: []
          }
        }
      }

 

雷达

  const shader = {
        vs: `varying vec3 vp;
        varying vec2 vUv;
   void main()
   {
        vp = position;
        vUv = uv;
    vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
    gl_Position = projectionMatrix * mvPosition;
   }`,
        fs: ` varying vec3 vp;
        varying vec2 vUv;
        uniform sampler2D texture;
        uniform vec3 color;
        uniform float radius;
        uniform float time;
        uniform float angle;
        float ag;
        const float PI = 3.1415946;
        bool getf(float x, float z) {
          return x * x + z * z <= radius * radius;
        }
        void main( void ) {
          ag = atan(vp.x, vp.z) - atan(1.0, 0.0); // atan(x/y) 返回的是-pi/2 -- pi/2  atan(x, y) 返回的是 -pi -- pi
          if (ag < 0.0) ag += 2.0 * PI;
          float m1 = mod(time, 2.0 * PI);
          float m2 = mod(angle + time, 2.0 * PI);
          float op = 0.0;
          vec4 colorSlef = texture2D( texture, vUv);
          vec4 shaderColor =  vec4( 0.0,  0.0,  0.0, 0.0 );
           if(getf(vp.x, vp.z)) {
              if( m1  < ag  && ag <= m2 ) {
                op = (ag - m1) / angle;
              }  
              else if( m1 > m2 && m1 <= ag  ) {
                op =  (ag - m1) / angle;
              }
              else if(m1 > m2 && ag < m2) {
                op = (angle - m2 + ag) / angle;
              }
           }
            shaderColor = vec4( color.r,  color.g,  color.b,  op);
            gl_FragColor = mix(colorSlef, shaderColor, op);
        }`,
        uniforms: {
          color: {
            type: 'c',
            value: new THREE.Color('red')
          },
          time: {
            type: 'f',
            value: 0
          },
          radius: {
            type: 'f',
            value: 200
          },
          angle: {
            type: 'f',
            value: Math.PI / 2
          },
          texture: {
            type: 't',
            value: texture
          }
        }
      }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
要展示柱状图饼图,您需要使用前端图表库,例如 ECharts 或 Chart.js。在 Node.js 中,您可以使用 MongoDB Node.js 驱动程序连接到 MongoDB 数据库并检索数据。 以下是大致的步骤: 1. 使用 MongoDB Node.js 驱动程序连接到 MongoDB 数据库。 2. 检索数据并将其转换为前端图表库所需的格式。 3. 通过 API 将数据传递给前端。 4. 在前端使用图表库创建柱状图饼图。 下面是一个基本的 Express 和 MongoDB 示例代码,用于连接到数据库和检索数据: ```javascript const express = require('express'); const MongoClient = require('mongodb').MongoClient; const app = express(); // 连接到 MongoDB 数据库 MongoClient.connect('mongodb://localhost:27017', function(err, client) { if (err) throw err; // 选择数据库 const db = client.db('mydb'); // 检索数据 db.collection('mycollection').find().toArray(function(err, result) { if (err) throw err; // 将数据传递给前端 app.get('/data', function(req, res) { res.json(result); }); }); }); app.listen(3000, function() { console.log('Server listening on port 3000'); }); ``` 在前端,您可以使用 ECharts 或 Chart.js 等库来创建图表。这里是一个使用 ECharts 创建柱状图饼图的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>柱状图饼图</title> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"></script> </head> <body> <div id="chart" style="width: 600px; height: 400px;"></div> <script> // 获取数据 fetch('/data') .then(response => response.json()) .then(data => { // 转换数据格式 const barData = data.map(item => [item.name, item.value]); const pieData = data.map(item => ({name: item.name, value: item.value})); // 创建柱状图 const barChart = echarts.init(document.getElementById('chart')); const barOption = { tooltip: {}, xAxis: {type: 'category', data: barData.map(item => item[0])}, yAxis: {type: 'value'}, series: [{type: 'bar', data: barData}] }; barChart.setOption(barOption); // 创建饼图 const pieChart = echarts.init(document.getElementById('chart')); const pieOption = { tooltip: {}, series: [{type: 'pie', data: pieData}] }; pieChart.setOption(pieOption); }); </script> </body> </html> ``` 请注意,此示例仅用于演示目的,您需要根据自己的需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

跃焱邵隼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值