Medication adherence is an important topic in chronic disease management. In this paper, we propose a framework from EHR data to cohort construction with drug ontology, to medication adherence assessment, and to data analytics based on adherence assessment. With a drug ontology based on Chinese guidelines for diabetes prevention and treatment, we construct a cohort of type 2 diabetes mellitus (T2DM) patients with anti-diabetic, anti-hypertensive, lipid-lowering, or anti-platelet drug treatment from a real world EHR data. We assess the medication adherence of the cohort with a multistate adherence measure. Multivariate logistic regression is used to find features that are associated with adherence to anti-diabetic drug treatment of T2DM patients. We also analyze the drug usage and hospital distribution at the first prescription. The experimental results show that patients visit to general hospital have better medication adherence than that of community healthcare centers (CHCs). The adherence to anti-diabetic drug treatment of T2DM patients is low, about 32.6%. Interventions should be made to help improve patients' medication adherence and treatment effectiveness.