折线图绘制代码:

#include<iostream>

//旧版本 固定管线
#include<Windows.h>
#include <GL/glut.h>
//新版本 可编程管线
/*#define GLEW_STATIC
#include <GL/glew.h>
#include<GLFW\glfw3.h>
*/ using namespace std; GLsizei winWidth = , winHeight = ;
GLint xRaster = , yRaster = ; GLubyte label[] = { 'J','a','n', 'F','e','b', 'M','a','r', 'A','p','r',
'M','a','y', 'J','u','n', 'J','u','l', 'A','u','g',
'S','e','p', 'O','c','t', 'N','o','v', 'D','e','c' }; GLint dataValue[] = { ,,,,,,,,,,, }; void init()
{
//窗口背景为白色
glClearColor(, , , );
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 610.0, 0.0, 500.0);
} void lineGraph()
{
GLint month, k;
GLint x = ; glClear(GL_COLOR_BUFFER_BIT);
//蓝色
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINE_STRIP); //画出折线段
for (k = ; k < ; k++) {
glVertex2i(x + k * , dataValue[k]);
}
glEnd(); //红色
glColor3f(1.0, 0.0, 0.0); //标注各点
for (k = ; k < ; k++) {
glRasterPos2i(xRaster + k * , dataValue[k] - );
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '*');
} //黑色
glColor3f(0.0, 0.0, 0.0); //横坐标说明
xRaster = ;
for (month = ; month < ; month++) {
glRasterPos2i(xRaster, yRaster);
for (k = * month;k < * month + ;k++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]);
xRaster += ;
}
}
glFlush();
} void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(, GLdouble(newWidth), , GLdouble(newHeight));
glClear(GL_COLOR_BUFFER_BIT);
} int main(int argc, char* argv[])
{ //对GLUT进行初始化,并处理所有的命令行参数
glutInit(&argc, argv);
//指定使用RGBA模式还是颜色索引模式,还可指定使用单缓冲还是双缓冲窗口
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
//指定了窗口左上角在屏幕上的位置
glutInitWindowPosition(, );
//制定了窗口大小(以像素为单位)
glutInitWindowSize(winWidth, winHeight);
//创建支持opengl渲染的窗口,在调用glutMainLoop函数前,窗口没有显示
glutCreateWindow("折线图");
init();
//显示回调函数
//每当GLUT确定一个窗口的内容需要重新显示时,通过glutDisplayFunc()注册的那个回调函数就会被执行
glutDisplayFunc(lineGraph);
glutReshapeFunc(winReshapeFcn);
//启动程序,所有窗口这时显示
glutMainLoop(); system("pause");
return ; }

运行结果:

柱状图代码:

void barChart()
{
GLint month, k; glClear(GL_COLOR_BUFFER_BIT); //红色
glColor3f(1.0, 0.0, 0.0);
for (k = ; k < ; k++) {
glRecti( + k * , , + k * , dataValue[k]);
} //黑色
glColor3f(0.0, 0.0, 0.0); //横坐标说明
xRaster = ;
for (month = ; month < ; month++) {
glRasterPos2i(xRaster, yRaster);
for (k = * month;k < * month + ;k++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, label[k]);
xRaster += ;
}
}
glFlush();
}

运行结果:

饼图代码:

#include<iostream>
#include <math.h>
//旧版本 固定管线
#include<Windows.h>
#include <GL/glut.h>
//新版本 可编程管线
/*#define GLEW_STATIC
#include <GL/glew.h>
#include<GLFW\glfw3.h>
*/ using namespace std; const GLdouble twoPi = 6.283185; class scrPt {
public:
scrPt() {
x = y = ;
}
GLint x, y;
void setCoords(GLint xCoordValue, GLint yCorrdValue) {
x = xCoordValue;
y = yCorrdValue;
}
GLint getx() const {
return x;
}
GLint gety() const {
return y;
}
void incrementx() {
x++;
}
void incrementy() {
y--;
} }; GLsizei winWidth = , winHeight = ; void init()
{
//窗口背景为白色
glClearColor(, , , );
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
} void setPixel(GLint xCoord, GLint yCoord)
{
glBegin(GL_POINTS);
glVertex2i(xCoord, yCoord);
glEnd();
} void circlePlotPoints(GLint xc, GLint yc, scrPt circPt)
{
setPixel(xc + circPt.getx(), yc + circPt.gety());
setPixel(xc - circPt.getx(), yc + circPt.gety());
setPixel(xc + circPt.getx(), yc - circPt.gety());
setPixel(xc - circPt.getx(), yc - circPt.gety());
setPixel(xc + circPt.gety(), yc + circPt.getx());
setPixel(xc - circPt.gety(), yc + circPt.getx());
setPixel(xc + circPt.gety(), yc - circPt.getx());
setPixel(xc - circPt.gety(), yc - circPt.getx());
} //中心画圆算法
void circleMidpoint(GLint xc, GLint yc, GLint radius)
{
scrPt circPt;
GLint p = - radius;//中点参数初值
circPt.setCoords(, radius);
circlePlotPoints(xc, yc, circPt);
while (circPt.getx() < circPt.gety()) {
circPt.incrementx();
if (p < )
p += * circPt.getx() + ;
else {
circPt.incrementy();
p += * (circPt.getx() - circPt.gety()) + ;
}
circlePlotPoints(xc, yc, circPt);
}
} void pieChart()
{
scrPt circCtr, piePt;
GLint radius = winWidth / ;
GLdouble sliceAngle, previousSliceAngle = 0.0; GLint k, nSlices = ; GLfloat dataValues[] = { 10.0,7.0,13.0,5.0,13.0,14.0,
3.0,16.0,5.0,3.0,17.0,8.0 };
GLfloat dataSum = 0.0; circCtr.x = winWidth / ;
circCtr.y = winHeight / ;
circleMidpoint(circCtr.x,circCtr.y, radius); for (k = ;k < nSlices;k++) {
dataSum += dataValues[k];
} for (k = ; k < ; k++) {
sliceAngle = twoPi * dataValues[k] / dataSum + previousSliceAngle;
piePt.x = circCtr.x + radius * cos(sliceAngle);
piePt.y = circCtr.y + radius * sin(sliceAngle);
glBegin(GL_LINES);
glVertex2i(circCtr.x, circCtr.y);
glVertex2i(piePt.x, piePt.y);
glEnd();
previousSliceAngle = sliceAngle;
} } void displayFcn()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
pieChart();
glFlush();
} void winReshapeFcn(GLint newWidth, GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, GLdouble(newWidth), 0.0, GLdouble(newHeight));
glClear(GL_COLOR_BUFFER_BIT); winWidth = newWidth;
winHeight = newHeight;
} int main(int argc, char* argv[])
{ //对GLUT进行初始化,并处理所有的命令行参数
glutInit(&argc, argv);
//指定使用RGBA模式还是颜色索引模式,还可指定使用单缓冲还是双缓冲窗口
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
//指定了窗口左上角在屏幕上的位置
glutInitWindowPosition(, );
//制定了窗口大小(以像素为单位)
glutInitWindowSize(winWidth, winHeight);
//创建支持opengl渲染的窗口,在调用glutMainLoop函数前,窗口没有显示
glutCreateWindow("Pie");
init();
//显示回调函数
//每当GLUT确定一个窗口的内容需要重新显示时,通过glutDisplayFunc()注册的那个回调函数就会被执行
glutDisplayFunc(displayFcn);
glutReshapeFunc(winReshapeFcn);
//启动程序,所有窗口这时显示
glutMainLoop(); system("pause");
return ; }

运行结果:

OpenGL——折线图柱状图饼图绘制的更多相关文章

  1. 百度推出的echarts,制表折线图柱状图饼图等的超级工具(转)

    一.简介: 1.绘制数据图表,有了它,想要网页上绘制个折线图.柱状图,从此easy. 2.使用这个百度的echarts.js插件,是通过把图片绘制在canvas上在显示在页面上. 官网对echarts ...

  2. G2 基本使用 折线图 柱状图 饼图 基本配置

    G2的基本使用 1.浏览器引入  <!-- 引入在线资源 --> <script src="https://gw.alipayobjects.com/os/lib/antv ...

  3. JavaScript数据可视化编程学习(一)Flotr2,包含简单的,柱状图,折线图,饼图,散点图

    一.基础柱状图 二.基础的折线图 三.基础的饼图 四.基础的散点图 一.基础柱状图 如果你还没有想好你的数据用什么类型的图表来展示你的数据,你应该首先考虑是否可以做成柱状图.柱状图可以表示数据的变化过 ...

  4. 安卓图表引擎AChartEngine(三) - 示例源码折线图、饼图和柱状图

    折线图: package org.achartengine.chartdemo.demo.chart; import java.util.ArrayList; import java.util.Lis ...

  5. HighCharts之2D柱状图、折线图和饼图的组合图

    HighCharts之2D柱状图.折线图和饼图的组合图 1.实例源码 ColumnLinePie.html: <!DOCTYPE html> <html> <head&g ...

  6. 利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图、折线图、饼图

    利用pandas读取Excel表格,用matplotlib.pyplot绘制直方图.折线图.饼图 数据: 折线图代码: import  pandas  as pdimport  matplotlib. ...

  7. 数据可视化(Echart) :柱状图、折线图、饼图等六种基本图表的特点及适用场合

    数据可视化(Echart) 柱状图.折线图.饼图等六种基本图表的特点及适用场合 参考网址 效果图 源码 <!DOCTYPE html> <html> <head> ...

  8. v-charts 绘制柱状图、条形图、水球图、雷达图、折线图+柱状图,附官网地址

    v-charts 官网地址:https://v-charts.js.org/#/ 柱状图: <template> <ve-histogram :data="chartDat ...

  9. ChartControl 折线图 柱状图

    添加折线图(柱状图) 拖动ChartControl到Form上 在Series Collection中添加Line(或Bar) DevExpress.XtraCharts.Series series1 ...

随机推荐

  1. 控制台获取AngularJS某个元素的Scope

    如何在控制台获取到某个元素的Scope呢? 假设,页面元素为: <label>Name:</label><input type="text" ng-m ...

  2. 你真的会用Gson吗?Gson使用指南(1)

    JSON (官网) 是一种文本形式的数据交换格式,它比XML更轻量.比二进制容易阅读和编写,调式也更加方便.其重要性不言而喻.解析和生成的方式很多,Java中最常用的类库有:JSON-Java.Gso ...

  3. c++中string类对象和字符数组之间的相互转换

    string类在c++中是一个模板类,位于名字空间std中,注意这里不是string.h,string.h是C字符串头文件. 将string类型转换为字符数组char arr[10];string s ...

  4. 基于Socket网络编程

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a2011480169/article/details/73602708 博客核心内容: 1.Sock ...

  5. asp.net mvc Session RedisSessionStateProvider锁的实现

    最近项目用到了RedisSessionStateProvider来保存session,发现比内存session慢,后来慢慢了解,发现asp.net session是有锁的.我在文章 你的项目真的需要S ...

  6. Python中文转拼音代码(支持全拼和首字母缩写)

    本文的代码,从https://github.com/cleverdeng/pinyin.py升级得来,针对原文的代码,做了以下升级:     1 2 3 4 1.可以传入参数firstcode:如果为 ...

  7. PRTG参考价格

    2010年的香港的网站上看到如下价格:http://kb.option-hk.com/?tag=prtg-network-monitor 什么才算一个sensor What counts as a s ...

  8. 阿里云服务器CentOS7怎么分区格式化/挂载硬盘

    一.在阿里云上购买了服务器的硬盘后就可以操作了,先看看硬盘情况: 硬盘vda是系统盘:vdb是在阿里云后台购买的另一块硬盘. 第一次使用要分区:fdisk /dev/vdb1 在提示符下依次输入:n+ ...

  9. V-rep学习笔记:转动关节2

    Torque or force mode: in this mode, the joint is simulated by the dynamics module, if and only if it ...

  10. 视觉SLAM中的数学基础 第三篇 李群与李代数

    视觉SLAM中的数学基础 第三篇 李群与李代数 前言 在SLAM中,除了表达3D旋转与位移之外,我们还要对它们进行估计,因为SLAM整个过程就是在不断地估计机器人的位姿与地图.为了做这件事,需要对变换 ...