我对此非常陌生,并希望你能够提供帮助。
我正在尝试从.txt文件在1个图表上绘制4组数据。目前,他们通过按下'['键单独显示,但理想情况下我希望它们显示在1和相同的图表上,不同的颜色代表每个组/列。我不知道如何修改此代码以启用此功能。
请参阅下面的代码和示例数据:
FloatTable data; // Stores data
float dataMin, dataMax; // dataMin and Max of %.
float plotX1, plotY1;
float plotX2, plotY2;
float labelX, labelY;
int rowCount;
int columnCount;
int currentColumn = 0;
int noMin, noMax;
int[]nos;
int noInterval = 1;
int volumeInterval = 5;
int volumeIntervalMinor = 1;
PFont plotFont;
void setup()
{
size(920, 500);
data = new FloatTable("AntiSocialBeh.txt");
rowCount = data.getRowCount();
columnCount = data.getColumnCount();
nos = int(data.getRowNames());
noMin = nos[0];
noMax = nos[nos.length - 1];
dataMin = 0;
dataMax = ceil(data.getTableMax() / volumeInterval) * volumeInterval;
//corners
plotX1 = 30;
plotX2 = width - 25;
labelX = 50;
plotY1 = 35;
plotY2 = height - 60;
labelY = height - 25;
plotFont = createFont("SansSerif", 10);
textFont(plotFont);
smooth();
}
void draw()
{
background(224);
fill(255);
rectMode(CORNERS);
noStroke();
rect(plotX1, plotY1, plotX2, plotY2);
drawTitle();
drawAxisLabels();
drawNosLabels();
drawVolumeLabels();
stroke(255, 0, 0);
noFill();
strokeWeight(5);
drawDataHighlight(currentColumn);
drawDataPoints(currentColumn);
}
void drawTitle() {
fill(0);
textSize(16);
textAlign(LEFT);
String title = data.getColumnName(currentColumn);
text(title, plotX1, plotY1 - 10);
}
void drawAxisLabels() {
fill(0);
textSize (10);
textAlign(CENTER);
text("Borough", (plotX1+plotX2)/2, labelY);
}
void drawNosLabels() { // = borough's labels
fill(0);
textSize(10);
textAlign(CENTER,TOP);
//draw grid
stroke(224);
strokeWeight(1);
for (int row = 0; row < rowCount; row++) {
if (nos[row] % noInterval == 0) {
float x = map(nos[row], noMin, noMax, plotX1, plotX2);
text(nos[row], x, plotY2 + 10);
line(x, plotY1, x, plotY2);
}
}
}
void drawVolumeLabels() {
fill(0);
textSize(10);
stroke(128);
strokeWeight(1);
for (float v = dataMin; v <= dataMax; v+= volumeIntervalMinor) {
float y = map(v, dataMin, dataMax, plotY2, plotY1);
if (v % volumeInterval == 0) { //if a major tick mark
if (v == dataMin) {
textAlign(RIGHT); //align by the bottom
}else if (v == dataMax){
textAlign(RIGHT, TOP); // align by the top
}else {
textAlign(RIGHT, CENTER); //center vertically
}
text(floor(v), plotX1 - 10, y);
line(plotX1 - 4,y,plotX1, y); //draw major tick
}
}
}
//Draw data as series of points
void drawDataPoints(int col) {
int rowCount = data.getRowCount();
for (int row = 0; row < rowCount; row++) {
if (data.isValid(row, col)) {
float value = data.getFloat(row, col);
float x = map(nos[row], noMin, noMax, plotX1, plotX2);
float y = map(value, dataMin, dataMax, plotY2, plotY1);
point(x,y);
}
}
}
void keyPressed()
{ if (key == '[') {
currentColumn--;
if (currentColumn < 0) {
currentColumn = columnCount -1;
}
}else if (key == ']') {
currentColumn++;
if (currentColumn == columnCount){
currentColumn = 0;
}
}
}
这是数据的样本 - 有33行代表特定地点的数据(此处显示为数字):在上面的代码中,“数字”代表x轴和% - y轴。我想颠倒它:制作%x轴并用地点名称替换数字作为y轴,遗憾的是我还不知道如何做到这一点。
我非常感谢你的帮助。
numbers% a % b % c % d
1 39.1 45.5 52.1 29.7
2 19.2 24.7 26.5 30
3 26 39.8 30.3 26.3
4 29.3 33.6 44.1 31
5 17.1 27.2 22.9 29.1
6 26.9 42.7 45 27.5
7 7 39.3 11.7 53.5
8 23.4 31.4 33.2 26.3
9 30 40.1 39.7 27.2
答案 0 :(得分:1)
基本上你想设置线条颜色,然后为每一列调用'drawDataPoints()'。
我不知道你正在使用什么库所以我真的不知道如何更改线条颜色,但我会定义一个Color数组(每列一个),所以类似于:
for (int column = 0; column < columnCount; column++) {
strokeColor(color[i]);
drawDataPoints(column);
}