2009年5月23日

輕鬆使用MS Chart Control - 樣式設定篇

這次來簡單的說明一下 Chart Control 的一些樣式設定吧

首先,先把焦點放到 Chart 屬性的 Appearance 分類
要修改Chart上的資料顯示顏色,可直接修改 Palette 屬性,下拉會有預設的多種樣式
若是想要自己設定顏色,就到 PaletteCustomColors 裡設定

不過坎尼這邊一直有個疑問,在 PaletteCustomColors 的說明裡

提到這個屬性比上面的 Palette 的屬性值還要優先
但為何兩個屬性同時設定時,出現的顏色卻是以 Palette 優先呢? (如下圖)

坎尼開了 vs2010 的 Chart Control 出來看,也是一樣
所以說明是一回事,顯示出來又是另一回事....
Anyway,大家有空再自行測試看看 (說不定是坎尼搞錯了?)

另外還有一種方法可以改變資料顯示的顏色
就是直接到 Series 裡修改,且目前實驗發現,這邊修改的 Style 優先權最高
下面可以看到坎尼加了個 Series2,並將顏色改為漸層

顏色為自訂的漸層色

最後來補充一下,上面的圖都有出現的外框
先打開 Chart 屬性裡的 BorderSkin
首先要選定 SkinStyle,接著再調整要展示的樣式

前幾張實際畫面的 Border Style 如下
(這裡為了展示,所以幾乎所有樣式都選了)


最後是 範例檔下載
範例展示畫面


其實這篇所講的樣式還只是冰山一角
未來有機會會再介紹其他樣式設定

2009年5月21日

輕鬆使用MS Chart Control - 資料繫結篇

之前坎尼有寫一篇 Chart Control 製作學生成績表
現在就來教一下如何使用 Chart Control 吧!
(安裝部分就跳過不講了,請自行參照上面的連結)

I.利用 SqlDataSource 控制項
首先,在頁面上拉進一個 Chart 和 SqlDataSource

將 Chart1 的 DataSourceID 屬性指定為剛剛拉進來的 SqlDataSource

接著選擇 Chart1 屬性裡的 Series 集合

選 Series1後,設定 XValueMember 及 YValueMembers
這邊由於有設定 SqlDataSource,所以兩個屬性都可以由下拉的方式選擇
(注意:X軸為類別,Y軸為數值

接著就是放到網頁上預覽啦


II.利用 ObjectDataSource 控制項
首先,在後端建立一個取得資料的方法,如下圖的 GetMyTable()
這邊坎尼先用人工的方式建立資料,並可回傳給 ObjectDataSource 使用

前端加入一個 ObjectDataSource
並將 ID 指定給 Chart1 的 DataSourceID 屬性
XValueMember 及 YValueMembers 的設定方式和前面相同

OK!!!大功告成!! (最後會有畫面

III.利用程式控制
其實這個方法已經在上次講的 範例 裡有提到過了
  • 先取得資料
  • 把資料設定給 Chart1.DataSource
  • Chart1.DataBind(); //打完收功

這邊要注意的是 ValueMember 已在前端設定,所以可以直接 DataBind()
否則要加入下圖的設定


範例檔下載 (只有 ObjectDataSource 及 程式控制部分,SqlDataSource請自行練習)

範例檔的執行畫面 (就長度來看,工程師的薪水似乎比較高?)


資料繫結給 Chart 控制項
最主要的就是 X 及 Y 的繫結欄位,且Y一定要數值型
另外目前都還只用一個 Series,或許未來會用多個 Series 來示範?

下次的主題是樣式設定,敬請期待 :D

2009年5月19日

visual studio 2010 beta 快照

距離上次提出 vs2010 CTP 版本至今大約也過了半年
總算是在本星期三可下載beta版本 下載點 (聽說星期三才有用)
這邊坎尼抓了幾張圖和大家分享一下

安裝畫面可以參考祭司的 VS 2010安裝畫面預覽
安裝完就是漂亮的 vs2010 start page
這次將整個 visual studio 利用 WPF 重新改寫
畫面真是愈來愈亮麗啦 (這裡很抱歉沒抓圖 Orz

首先是 new project 的畫面

上面一樣有 multi-target 選項,當然支援到 .NET Framework 4.0 beta
另外還有 2.0、3.0、3.5 可供使用者選擇
左方的專案樣版也提供了 SharePoint 及 Silverlight 開發樣版
但 MVC 這次則不包含在vs2010 beta 版本裡,而且聽說裝了還會出問題
(若還是想裝MVC的人可以參考這篇

另外這次在UI上方多了個 Sort 下拉選項,可以將專案樣版依名稱做排序

接著坎尼開了個 Web Application 想比較一下差異性
下圖左方為 vs 2010 的預設 Toolbox,右方則為 vs 2008
以全部收合的大類來看是完全一樣的 (坎尼另外加進來的 item 不算

standard 一打開坎尼就發現多了 DynamicHyperLink 的 Control

Data工具箱打開則是多了 MS Chart Control
但由於坎尼早就裝了,所以這裡的工具都一樣

就在坎尼逐一對照之下,發現其他工具箱裡的控制項完全一樣 (除了版本號改為4.0
所以此次在控制項方面的改變不大
這次外功目前看來尚無強力新招,但內力據坎尼看到的倒是增加了不少
如加強 Ajax Library、Session State 改善等 (詳情請參照 這裡

最後提一個坎尼從CTP就十分尬意的功能:code snippets of javascript
下圖是在打完 function 後,按下 tab 鍵跳出的畫面

當然,for迴圈等基本語法也有支援

以後寫程式的速度又可加快不少

有興趣的人別忘了星期三 (應該是美國時間) 上網下載 vs 2010 beta1喔 :)

2009年5月9日

New World Community Grid Project – Influenza Antiviral Drug Search

距離上次 World Community Grid 啟動的新專案 – Help Fight Childhood Cancer 不久,前幾天收到 World Community Grid 寄來的信,最近開始了一個新的 Research Project
- Influenza Antiviral Drug Search,顧名思義,就是要研發對抗流感的藥物,主要針對那些已經對一般藥物產生抗藥性的強大流感病毒。

如果你不想讓你的 CPU 繼續吃閒飯,那麼可以藉由這個機會加入這個人類對抗病毒的大戰!!

Mail 的內容如下:

Title: World Community Grid - You Can Help Fight Influenza!
World Community Grid has started a new research project that will use your donated computer time to find new drugs that can stop the spread of an influenza infection in an individual. The research will specifically address the influenza strains that have become drug resistant, as well as new strains that are appearing. Your computer will help identify the chemical compounds that are the best candidates. This will accelerate the efforts to develop treatments that would be useful in managing seasonal influenza outbreaks and future influenza epidemics.
If you are currently contributing and want to check to see if you are contributing to this project, click here. If you are no longer contributing but would like to contribute to this project, please click here.

For more information about the Influenza Antiviral Drug Search project or other projects running on World Community Grid, please click here.

Influenza Antiviral Drug Search 計畫首頁的說明如下:

Project Status and Findings: 
If you have comments or questions about this project, please visit the Influenza Antiviral Drug Search forum. For specific questions about the project, please go to the Influenza Antiviral Drug Search FAQs.

Mission
The mission of the Influenza Antiviral Drug Search project is to find new drugs that can stop the spread of an influenza infection in the body. The research will specifically address the influenza strains that have become drug resistant as well as new strains that are appearing. Identifying the chemical compounds that are the best candidates will accelerate the efforts to develop treatments that would be useful in managing seasonal influenza outbreaks, and future influenza epidemics and even pandemics.

Significance
Hundreds of thousands of people around the world die each year as a result of influenza infections. If a particularly virulent strain emerges that is readily transmitted among humans, a pandemic can potentially kill millions of people. The influenza virus mutates rapidly and therefore new varieties of influenza appear each year. Influenza vaccines are designed to anticipate the strains likely to be prevalent for the next flu season but can often miss newly evolved varieties. While drugs such as oseltamivir (commercial name Tamiflu) and zanamivir (commercial name Relenza) help keep influenza from spreading in the body, they do not work for all types of influenza and new drug resistant forms of influenza evolve and cannot be treated with these drugs. When drug resistant strains emerge, manufacturers designing vaccines may not be able to respond quickly enough to prevent a large outbreak of the disease. This project will search for promising new drug leads that stop the replication of the influenza viruses within the body, potentially helping patients who are not immune to a particular form of influenza. The discovery of both broad-spectrum and specific antiviral drugs is expected to significantly improve global health.

Approach
One promising approach to combat these viruses and prevent them from causing disease is to develop new drugs that inhibit neuraminidase (N1, N2, etc), NS1 protein, hemagglutinin, and possibly other targets that influenza needs to spread in the body. Using the known chemical structures of these target molecules, the project will perform virtual chemistry experiments and determine which of millions of known compounds attach to these target molecules in a manner that can disable or inhibit them, thus potentially keeping the influenza virus from spreading in the body. These experiments are performed using a program called AutoDock, from the Scripps Research Institute. This software is used for several other projects on World Community Grid. These include FightAIDS@Home, Discovering Dengue Drugs - Together, and Help Fight Childhood Cancer projects. Once drug candidates are identified using World Community Grid, then additional laboratory work, testing and ultimately drug trials will be required. This will likely not lead to any immediate treatments for influenza during the current H1N1 outbreak. However, influenza viruses cause recurrent seasonal disease outbreaks and have the potential for evolving into dangerous forms. Thus, proceeding on this project at this time will give us a better chance to be prepared for more serious outbreaks in the future.
In an effort to spread the words about WCG, I will include a link to my last post about WCG – here it is: Help Fight Childhood Cancer

2009年4月27日

自製無用小工具系列 - 利用 Excel VBA 產生大量亂數日期

這個工具起因於某日,坎尼要建立測試資料,但日期必須打散
極度懶惰的坎尼就用 Excel 做了個亂數跑日期的小程式

「為什麼要用 Excel 哩?」『簡單、好用、又好玩』

第一、要產生某種格式的資料很快很簡單
第二、啟動不像 visual studio 那麼久
第三、不用再重新拉UI,每個儲存格都是 TextBox

要寫 VBA 之前,先知道 VBA 編輯器要怎麼開啟,見下圖
(快速鍵為 ALT + F11)

再來是原始資料的創建
先全選 Sheet1 的儲存格,右鍵選儲存格內容,將儲存格轉換為文字格式
A1儲存格輸入 1995,B1輸入01,C1輸入01
再利用 Excel 的自動填滿選項功能建立如下圖的資料 (這時候就知道Excel的好用了)

接著一樣把 Sheet2所有的儲存格改為文字格式
以免產生出來的資料,Excel會自以為聰明的幫你改格式 :D

資料準備好之後,就按下 ALT + F11 進入 VBA 編輯器
再來就是寫程式了,當然,是要用 VB 的語法

下圖先用兩個 For 迴圈決定要產生的資料量 (I、J分別為 Column及Row)
新增三個亂數值,分別決定年月日的亂數 Index
再將亂數產生的值組合起來放進 Sheet2 的儲存格裡

寫完程式之後,直接點執行鍵即可 (編輯器上面的綠三角形)
再切到Sheet2就可以看到滿滿的亂數日期了


Excel 檔下載 (若要使用坎尼的檔案,記得打開巨集)

其實這個程式把Sheet1的資料來源改一改 (亂數index的range也可以更改)
就可以變成 電影名稱產生器、武功名稱產生器...

最後補充一下,坎尼程式裡年的 index 亂數設錯了 (應該為 14)
所以不會有 2007 及 2008 年的資料

2009年4月19日

微軟內部開始實驗的新 QA 機制:Bug Goal

這個月初去微軟位於南港的創新中心(Microsoft Innovation Center, MIC)上課,
在第三天的課程中聽到一個很有趣的觀念,叫做 Bug Goal,以下就來說說這是什麼。

在上一篇文章:微軟的組織架構 & Developer 的工作態度中曾經提到,基本上每個微軟的 team 都是由 PM、dev、以及 tester 組成,而 Bug Goal 就是由 Tester 這邊提出來的新觀念,目前正在美國總部小規模實驗中,如果成效良好,可能會全面推廣。

顧名思義,Bug Goal 指的是 bug 的指標,更清楚一點的說,是指在每個功能區塊中預期會產生多少 bug。一開始聽到這個觀念大家都覺得不可思議,所以花了不少時間討論。

Bug Goal 一開始大概只能靠 manager 從過去的經驗中做一個接近直覺的估計,但是隨著經驗的累積,理論上這個值應該愈來愈精準。訂定 Bug Goal 的目的如下:
  • 激勵 tester 在程式中找出數量足夠的、有意義的 bug,目標就是 Bug Goal 設定的數量。
  • 激勵 dev 寫出更高品質的 code,讓 tester 沒辦法抓出足夠的 bug 來達到 Bug Goal 設定的數量。
    (有點像是故意在 dev 和 tester 之間建立一個良性的競賽,目的在提高品質)
  • 讓 PM 多了一種衡量程式品質的數據,當某一個功能根據過去的經驗,是很容易產生大量的 bug,但專案執行一段時間以後 tester 卻幾乎沒有找到任何 bug 的時候,就應該主動更深入的了解一下專案執行的狀況,當然有可能是程式的品質真的非常好,但有沒有可能是 teser 在偷懶?或者 dev 偷偷把功能簡化,所以程式變得很簡單,不容易出錯?
會影響 Bug Goal 的因素包括:
  • 功能的複雜度
  • dev 的熟練度
  • tester 的熟練度
  • 需求變化的程度
  • ...(可能還有很多其他的因素,總之是要綜合考量這些因素)
有一點要強調的是,tester 找到的 bug count 有沒有 meet Bug Goal 絕對不是一個適合用來衡量 tester 能力的指標,例如 bug 的品質就是一項重要的因素(有沒有濫竽充數?),而有經驗的 tester 在 system design 的階段就會密切參與討論,貢獻自己的專業知識,提高系統的 testability,以及避免可能很難測試的設計,這些都可能造成 bug count 的降低,自然就達不到預先設定的 Bug Goal。

反過來說,達不到 Bug Goal 可以看成是一個好現象(前提是沒有人掩耳盜鈴),而且整個 team 長遠的目標應該是要讓 Bug Goal 越來越低,這表示 team member 的成熟度很高,可以產出高品質的 code。(這當然是個非常遙遠的理想)

2009年4月18日

[InfoQ QCon] 10 ways to improve our code

早上看了這個 talk (InfoQ Video link, Summary),主題是講 10 個改善程式碼品質的原則,有些不錯的地方值得記錄一下。

主講人 Neal Ford 先生是 ThoughtWorks 的架構師(跟 Martin Fowler 同一家公司),
演講的內容是從他寫的書:The Productive Programmer 裡面整理出來的,他講的很流暢,也蠻幽默的,演講時間大約一小時,投影片可以在這裡下載。

由於重點在 Summary 裡面都有列出來了,因此以下採用補充的形式記錄我印象深刻的部份,請搭配 Summary 服用。

Composed Method & SLAP (Single Level of Abstraction Principle)
Composed Method 這個原則講的是透過 method 的切割,盡可能的縮短每個 method 的行數,最後完整的 class 會由很多小小的 method 組成。

以切割 method 的方式來縮短行數的目的在於將程式碼的權責劃分清楚,以便達到最大程度的 reuse。但是在設計上也不應該為了切割而切割,此時可利用 SLAP 作為切割 method 時的 guideline。

SLAP 看來是 Neal 發明的縮寫,在 Wikipedia 上目前查不到,原始出處就是他寫的 The Productive Programmer,另外可以參考 Microsoft Architect Journal 2004 年 7 月號:Secrets of Great Architects(Levels of Abstraction: A Powerful Weapon for All Engineers)。

簡單的說,程式碼本來就是很抽象的,但還是可以區分出不同的抽象層次。最粗略的分法是,對 DB 的操作歸類為系統中較底層的程式碼,而 Domain Logic 則是系統的核心,UI 則是系統與使用者互動的介面,因此這三個層次的程式碼不應該混淆在同一個 method 中,就算不抽離到其他的 class 中,起碼也要拆解成不同的 method。

以下是同一個 method 中包含多個抽象層次的 pseudo code(節錄自投影片):
addOrder(WithMixedLevelOfAbstraction)

在這個未經 refactor 的版本中,可以看到非常詳細的操作 DB、Transaction 的程式碼,跟此 method 要處理的「新增訂單」Domain Logic 混雜在一起,因此閱讀的時候你的”心智”(mind)要多次在不同的抽象層次間的切換(mental shift),這種額外的負擔會造成閱讀程式碼時的不順暢。

以下是 refactor 之後的結果:
addOrder(WithSeparatedLevelOfAbstraction)

經過 refactor 後程式碼明顯縮短很多,而且現在此 method 中的抽象層次(大致)是一致的,如「completeTransaction」、「rollbackTransactionFor」等等的 method 不但名稱更有 business 上的意義,也隱藏了與 business 無關的、底層 API call(操作 DB、Transaction), 因此這段程式碼閱讀起來比上一個版本順暢很多,一行一行的順著讀下來比較接近日常生活的會話:
(撰寫此 method 的目的:新增訂單)
  1. 建立所需的 DB Connection 和 SQL Statement。
  2. 開啟 Connection 和準備 SQL Statement。
  3. 設定 transaction 的狀態。
  4. 新增一筆訂單。
  5. 設定訂單號碼。
  6. 將購物車中的物品資料寫入訂單。
  7. 完成交易。
Question Authority
這個原則中文應該可以稱為「挑戰權威」吧!例如挑戰 coding convention,或者(公司內)常見的系統設計方式。Neal 舉的例子是一個有名的猴子實驗,我找到一篇文章:The Origin of Tradition: A Monkey Experiment (圖文說明),另外很久之前在書上看過一篇類似的文章,我自己打字以後做成 PDF 檔,可以在這裡下載(這都是文字,沒有圖,但是內容比較充實)。
比較有趣的是上面那篇我另外找的文章裡面有引述愛因斯坦的話:

Only two things are infinite, the universe and human stupidity, and I’m not sure about the former.
                                                                                               -Albert Einstein

建立 convention 對 productivity 是一件好事,但是深入的了解 convention 形成的原因,才有辦法在適當的時候打破 convention,以新的方法做事情,創造更大的價值!

詳細內容就不贅述了,不管這是真的實驗還是一個故事,總之很發人深省!

Use the Anti-Objects Pattern
在這裡 Neal 利用早年大型機台上的老遊戲 – Pac-Man(中譯:小精靈)來解釋何謂 Anti-Objects Pattern。在 Wikipedia 上有個 entry 就叫做 Antiobjects,剛好裡面就有講到 Pac-Man(警告!以下資訊可能會破壞你對小精靈這款遊戲美好的回憶 XD):
The notion of antiobjects is a computational metaphor useful to conceptualize and solve hard problems by swapping computational foreground and background.
If we implement, as part of a Pac-Man game, a ghost we are tempted to think of the necessary behavior associated with the ghost object.
Antiobjects turn things on their head. In the case of Pacman we put the main computation into the maze.
                                                                                                            - Wikipedia


在當年運算能力十分缺乏的平台上,是無法將「追逐小精靈」的 AI 設計到「鬼」這個 object 上的(由鬼來計算迷宮中的 shortest path ),因此反過來設計者把 AI 設計到迷宮的地板上去,並且提出一個 Pac-Man Scent(小精靈的氣味)的觀念,也就是目前小精靈所在的那塊地板氣味最強,而這個氣味是交由迷宮的地板來記錄的,當小精靈遠離這塊地板時,它的氣味就逐漸減弱。

因此鬼實際上沒辦法知道小精靈的位置,鬼都是漫無目的的瞎逛,若剛好踩到一段有小精靈氣味殘留的地板,才會一路往氣味更強的方向去「追殺」小精靈。

透過 antiobjects 可以簡化 AI 的設計,另外也是訓練你如何 think outside of the box 的好方法喔!

最後來回憶一下經典的小精靈遊戲畫面吧!
6

Google Spreadsheet 裡用規則運算式

最近因為工作關係,遇到要用 Google Form 及 Google Sheet 所以研究了 Google Sheet 裡的一些 function 怎麼用 首先,分享一下如何在 Google Sheet 裡用規則運算 :D