วันศุกร์ที่ 9 สิงหาคม พ.ศ. 2556

Barchart.

Percentage of population aged 6 years and over used computer/ Internet by length
of access last week.



//Array คือ รูปแบบการก็บข้อมูลหลายๆตัวที่มีชนิดของข้อมูลเหมือนกันไว้ด้วยกันเป็นชุด โดยเข้าถึงข้อมูลแต่ละตัวใน Array ได้โดยใช้ index เริ่มต้นที่0
float [][]p= { //สร้างตัวแปร Array 2 มิติ ชนิดจำนวนทศนิยม และกำหนดค่า
  {
    5.7, 21.2, 36.6, 23.1, 13.4
  }
  , {
    6.7, 24.7, 37.3, 21.7, 9.6
  }
};

String[]a= {  
  "Not used", "<1 hours", "1-2 hours", "2-4 hours", "Moe than 4 hours"
}; //เป็นการประกาศตัวแปรเป็น Array ชนิดข้อความ ชื่อ a ซึ่งตัวแปรชนิดนี้จะเก็บค่าได้หลายตัวโดยมีตำแหน่ง index เป็นตัวกำกับ

float average=0; //ตัวแปรที่ใช้ในการเก็บค่าเฉลี่ย
int count =0;
int x=40;
int y =350;
int i=0;
float max= p[0][0]; //ตัวแปรที่ใช้ในการเก็บค่า max
float min= p[0][0]; //ตัวแปรที่ใช้ในการเก็บค่า min

void setup() { //เป็นฟังก์ชันแรกที่ถูกกำหนดไว้แล้วให้ถูกเรียกใช้งานเมื่อโปรแกรมเริ่มทำงาน ภายในมีคำสั่งต่างๆโดยเราสามารถใส่เข้าไปเองได้ใน { } จะถูกเรียกใช้โดยอัตโนมัติและเพียงครั้งเดียว
  size(500, 500);
  background(0); //ใส่สีดำให้กับพื้นหลัง
  stroke(255); //stroke คือ การใส่สีให้กับเส้น กำหนดค่าได้ดังนี้ (แดง,เขียว,น้ำเงิน) จากตัวอย่าง stroke(255); จะได้สีขาว
  line(x, y, x+420, y);
  while (count<a.length) {
    if (p[i][count]<min) {
      min=p[i][count];
    }
    if (p[i][count]>max) {
      max=p[i][count];
    }
    if (count==a.length) {
      i=i+1;
    }
    average = average+p[i][count];

    fill(0, 255, 127); //green color
    rect(x+10, y, 30, -p[i][count]*5);
    text(p[0][count], x, (y-p[0][count]*5)-10);
    fill(255, 20, 147); //pink color
    rect(x+45, y, 30, -p[i+1][count]*5);
    text(p[i+1][count], x+40, (y-p[i+1][count]*5)-10);
    fill(255);
    text(a[count], x+20, y+20);
    x=x+80; 
    count=count+1;
  }
  fill(255, 0, 0); //red color
  rect(50, 50, 110, 70);
  fill(255);
  text("Max is"+" "+max, 60, 70); //แสดงข้อความออกมาทางCanvas
  text("Min is"+" "+min, 60, 90);
  text("Average is"+" "+average/a.length, 60, 110);
  
  fill(255); //white color
  text("Computer", 380, 410);
  text("Internet", 380, 430);
  fill(0, 255, 127); //green color
  rect(360, 400, 10, 10);
  fill(255, 20, 147); //pink color
  rect(360, 420, 10, 10);
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น